poj 1338 Ugly Numbers

 原题链接:http://poj.org/problem?id=1338

优先队列的应用,如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<queue>
 4 #include<functional>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 const int Max_N = 1510;
10 ll ans[Max_N];
11 void init(){
12     int arr[4] = { 2, 3, 5 };
13     priority_queue<ll, vector<ll>, greater<ll> > que;
14     que.push(1);
15     for (int i = 1; i <= 1500;){
16         ll t = que.top();
17         que.pop(), ans[i] = t;
18         if (ans[i] == ans[i - 1]){
19             continue;
20         } else {
21             i++;
22         }
23         for (int j = 0; j < 3; j++) que.push((ll)(t * arr[j]));
24     }
25 }
26 int main(){
27     init();
28     int n;
29     while (~scanf("%d", &n) && n) printf("%lld
", ans[n]);
30     return 0;
31 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4462698.html