UVA-136Ugly numbers

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500’th ugly number. Input There is no input to this program. Output Output should consist of a single line as shown below, with ‘’ replaced by the number computed. Sample Output The 1500'th ugly number is .

#include<bits/stdc++.h>
int b[3]={2,3,5};
using namespace std;

int main()
{
    set<long long>s;
    priority_queue<long long,vector<long long>,greater<long long> >que;
    que.push(1);
    s.insert(1);
    int k;
    for(k=1;;k++)
    {   if(k==1500){ printf("The 1500'th ugly number is %lld.\n",que.top());return 0; }
        long long cnt=que.top();
        que.pop();
        for(int i=0;i<=2;i++)
        {
            long long z=cnt*(long long) b[i];


            if(!s.count(z)){ s.insert(z); que.push(z); }
        }
    }


    return 0;

}

 

posted @ 2018-07-23 21:51  StarHai  阅读(162)  评论(0编辑  收藏  举报