原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058
优先队列的应用,如下:
1 #include<functional> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 using std::vector; 9 using std::greater; 10 using std::priority_queue; 11 typedef unsigned long long ull; 12 const int Max_N = 5843; 13 ull ans[Max_N]; 14 void init() { 15 int arr[5] = { 2, 3, 5, 7 }; 16 priority_queue<ull, vector<ull>, greater<ull> > que; 17 que.push(1); 18 for (int i = 1; i <= Max_N;) { 19 ull t = que.top(); 20 que.pop(), ans[i] = t; 21 if (ans[i] == ans[i - 1]) { 22 continue; 23 } else { 24 i++; 25 } 26 for (int j = 0; j < 4; j++) que.push((ull)(t * arr[j])); 27 } 28 } 29 int main(){ 30 init(); 31 int n; 32 while (~scanf("%d", &n) && n) { 33 if (n % 10 == 1 && n % 100 != 11) 34 printf("The %dst humble number is %lld. ", n, ans[n]); 35 else if (n % 10 == 2 && n % 100 != 12) 36 printf("The %dnd humble number is %lld. ", n, ans[n]); 37 else if (n % 10 == 3 && n % 100 != 13) 38 printf("The %drd humble number is %lld. ", n, ans[n]); 39 else printf("The %dth humble number is %lld. ", n, ans[n]); 40 } 41 return 0; 42 }