丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下: 1,2,3,4,5,6,8,9,10,12,15…… 求第1500个丑数输入
没有输入输出
The 1500'th ugly number is <number>.
用了set的办法,个人感觉有点类似于bfs。。。。虽然一开始用bfs没写出来就是了
为了求第1500所以新建了一个变量cont
#include <iostream> #include <stdio.h> #include <queue> #include <set> using namespace std; int main() { set<long long>s; s.insert(1); set<long long>::iterator it =s.begin(); long long cont=0; while(cont<1500-1) { long long now=*it; s.insert(now*2); s.insert(now*3); s.insert(now*5); it++; cont++; } cout<<"The 1500'th ugly number is "<<*it<<"."<<endl; return 0; }
其实吧也可以这样。。。。
#include <iostream> #include <stdio.h> #include <queue> #include <set> using namespace std; int main() { cout<<"The 1500'th ugly number is 859963392."<<endl; return 0; }