所谓H数是指:仅包含质因子3,5,7,11的数。
例如:3,5,7,9,11均为H数,但12不是H数,......
问题:给出一个N(1~3000)
,求出由小到大的第N个H数。
例如:当N=3时,输出:7
https://blog.csdn.net/Fly_as_tadpole/article/details/82705774
#include <iostream>
#include <algorithm>
using namespace std;
int nthNumber(int n) {
int res[3001];
memset(res, 0, sizeof(res));
res[1] = 1;
int index3 = 1, index5 = 1, index7 = 1,index11=1;
for (int i = 2;i <=n;i++) {
res[i] = min(res[index11]*11,min(res[index7] * 7, min(res[index5] * 5, res[index3] * 3)));
if (res[i] == res[index3] * 3)
index3 += 1;
if (res[i] == res[index5] * 5)
index5 += 1;
if (res[i] == res[index7] * 7)
index7 += 1;
if (res[i] == res[index11] * 11)
index11 += 1;
}
return res[n];
}
int main() {
int n;
cin >> n;
cout<<nthNumber(n + 1)<<endl;
return 0;
}