丑数(UVa136)

  

  题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page=show_problem&problem=72

C++11代码如下:

 1 #include<iostream>
 2 #include<vector>
 3 #include<set>
 4 #include<queue>
 5 using namespace std;
 6 
 7 typedef long long LL;
 8 const int coeff[3] = { 2,3,5 };
 9 int main() {
10     priority_queue<LL, vector<LL>, greater<LL> >pq;
11     set<LL>s;
12     pq.push(1);
13     s.insert(1);
14     for (int i = 1;; i++) {
15         LL x = pq.top(); pq.pop();
16         if (i == 1500) { 
17             cout << "The 1500'th ugly number is " << x << '.' << endl;
18             break;
19         }
20         for (int j = 0; j < 3; j++) {
21             LL x2 = x * coeff[j];
22             if (!s.count(x2)) { 
23                 s.insert(x2); 
24                 pq.push(x2);
25             }
26         }
27     }
28     return 0;
29 }
posted on 2018-07-06 21:49  Pink.Pig  阅读(463)  评论(0编辑  收藏  举报