POJ1338丑数(uva136)
UVA书上用的set,poj上这个题我用的vector。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstdlib> 7 #include <cstring> 8 #include <queue> 9 #include <cstdio> 10 #include <cstdlib> 11 #include <set> 12 #include <sstream> 13 14 using namespace std; 15 const int a[3]={2,3,5}; 16 typedef long long LL; 17 18 int main() 19 { 20 priority_queue<LL, vector<LL>,greater<LL> > pq; 21 vector<LL> s; 22 pq.push(1); 23 s.push_back(1); 24 int i,j,m; 25 for(i=1;;i++) 26 { 27 LL y = pq.top(); 28 pq.pop(); 29 if(i==1500)break; 30 for(j=0;j<3;j++) 31 { 32 LL x = y*a[j]; 33 if(find(s.begin(),s.end(),x)==s.end()) 34 { 35 s.push_back(x); 36 pq.push(x); 37 } 38 } 39 } 40 sort(s.begin(),s.end()); 41 while(cin >> m && m) 42 cout << s[m-1] << endl; 43 return 0; 44 }