1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // implement by priority queue using STL set // CPU time = 0.008 #include <cstdio> #include <set> using namespace std; int main(){ int counter=0; long long int tmp; set<long long int> ugly_set; set<long long int>::iterator i; ugly_set.insert(1); while(counter<1500){ i=ugly_set.begin(); tmp=*i; ugly_set.insert(tmp*2); ugly_set.insert(tmp*3); ugly_set.insert(tmp*5); ugly_set.erase(tmp); counter++; } printf("The 1500'th ugly number is %lld.\n", tmp); } |
Direct link: https://paste.plurk.com/show/59601