G - Ugly Numbers UVA - 136

丑数是指不能被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;
}

猜你喜欢

转载自blog.csdn.net/W349652743/article/details/80355546