| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 20249 | Accepted: 9003 |
Description
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
Input
Output
Sample Input
1 2 9 0
Sample Output
1 2 10
Source
#include <cstring>
#include <iostream>
#include <algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
int main()
{
int n;
int c[3]={2,3,5};
while(scanf("%d",&n)&&n)
{
priority_queue<LL,vector<LL>,greater<LL> > pq;
set<LL> s;
pq.push(1);
s.insert(1);
for(int i=1;;i++)
{
LL x=pq.top();pq.pop();
if(i==n)
{
printf("%lld\n",x);
break;
}
for(int j=0;j<3;j++)
if(!s.count(c[j]*x))
{
s.insert(c[j]*x);
pq.push(c[j]*x);
}
}
}
}
2629



被折叠的 条评论
为什么被折叠?



