描述
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
输入
输入一个正整数N,0<N<10000
输出
输出一个正整数S,S为第N个丑数
输入样例
1 2 10
输出样例
1 2 12
题解:https://blog.youkuaiyun.com/leex_brave/article/details/51766194
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 10010;
ll a[maxn], n;
void Find(){
int id2 = 0, id3 = 0, id5 = 0;
int id = 0;
a[0] = 1;
while(id < 10000){
ll t = min(a[id2]*2, min(a[id3]*3, a[id5]*5));
if(t == a[id2]*2) id2++;
if(t == a[id3]*3) id3++;
if(t == a[id5]*5) id5++;
a[++id] = t;
}
}
int main()
{
Find();
while(cin >> n){
cout << a[n-1] << endl;
}
return 0;
}