poj 1338 ugly numbers

来源:互联网 发布:网络节点 游戏 编辑:程序博客网 时间:2024/05/21 09:57
Ugly Numbers
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 20249 Accepted: 9003

Description

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, ... 
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

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

Output

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

Sample Input

1290

Sample Output

1210

Source

New Zealand 1990 Division I,UVA 136


优先队列和set的妙用

#include <cstdio>
#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);
         }
     }
    }
}
0 0