#include <iostream>
#include <queue>
using namespace std;
int min(int a, int b)
{
return a < b ? a : b;
}
int getKthNumberBy357(int k)
{
if (k < 0)
{
return 0;
}
int v = 0;
queue<int> q3, q5, q7;
q3.push(1);
for (int i = 0; i <= k; i++)
{
int v3 = q3.size() > 0 ? q3.front() : INT_MAX;
int v5 = q5.size() > 0 ? q5.front() : INT_MAX;
int v7 = q7.size() > 0 ? q7.front() : INT_MAX;
v = min(v3, min(v5, v7));
if (v == v3)
{
q3.pop();
q3.push(3 * v);
q5.push(5 * v);
}
else if (v == v5)
{
q5.pop();
q5.push(5 * v);
}
else
{
q7.pop();
}
q7.push(7 * v);
}
return v;
}
int main()
{
for (int i = 0; i <= 20; i++)
{
cout << getKthNumberBy357(i) << endl;
}
getchar();
return 0;
}
求出第k个素因子只有3,5,7的数字
最新推荐文章于 2022-09-29 10:15:50 发布