题目描述

如果一个数的素因子只包含2,3,5或7,那么我们把这种数叫做丑数。序列1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27...展示了前20个丑数。
请你编程寻找这个序列中的第n个元素。

输入

输入包含多组测试数据。每组输入为一个整数n(1<=n<=5842),当n=0时,输入结束。

输出

对于每组输入,输出一行“The nth humble number is number.”。里面的n由输入中的n值替换,“st”,“nd”,“rd”和“th”这些序数结尾的用法参照输出样例。

样例输入

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

样例输出

The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.
  1 #define Question2
  2 #ifdef Question2
  3 
  4 #define Debug
  5 
  6 #ifdef Debug
  7 #include <ctime>
  8 #endif
  9 
 10 #include <iostream>
 11 #include <cstdio>
 12 #include <queue>
 13 #include <string>
 14 
 15 using namespace std;
 16 
 17 /* 获得后缀 */
 18 string getSuffix(int num);
 19 /* 建立检索表 */
 20 void getAns();
 21 /* 检索表:用向量表示检索表 */
 22 vector<long long> vec;
 23 
 24 int main()
 25 {
 26 /* 调试代码 */
 27 #ifdef Debug
 28     freopen("in.txt", "r", stdin);
 29     clock_t start, finish;
 30     double totaltime;
 31     start = clock();
 32 #endif
 33 
 34     int num;
 35     getAns();
 36 
 37     /* while (cin >> num && num)
 38     {
 39         cout << "The " << num << getSuffix(num)
 40              << " humble number is " << vec[num - 1] << "." << endl;
 41     } */
 42     for (int i = 5750; i <= 5842; i++)
 43     {
 44         cout << i << ":" << vec[i-1] << endl;
 45     }
 46 
 47 /* 调试代码 */
 48 #ifdef Debug
 49     finish = clock();
 50     totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
 51     cout << "Time : " << totaltime * 1000 << " ms" << endl;
 52 #endif
 53 
 54     return 0;
 55 }
 56 
 57 string getSuffix(int num)
 58 {
 59     num %= 100;
 60     if (num == 1)
 61         return "st";
 62     else if (num == 2)
 63         return "nd";
 64     else if (num == 3)
 65         return "rd";
 66     else if (num > 13)
 67     {
 68         int last = num % 10;
 69         if (last == 1)
 70             return "st";
 71         else if (last == 2)
 72             return "nd";
 73         else if (last == 3)
 74             return "rd";
 75         else
 76             return "th";
 77     }
 78     else
 79         return "th";
 80     return "";
 81 }
 82 
 83 void getAns()
 84 {
 85     /* 优先队列 */
 86     priority_queue<long long, vector<long long>, greater<long long> > priQue;
 87     vec.clear();
 88     vec.push_back(1);
 89     long long temp = 1;
 90     priQue.push(2);
 91     priQue.push(3);
 92     priQue.push(5);
 93     priQue.push(7);
 94     while (1)
 95     {
 96         temp = priQue.top();
 97         priQue.pop();
 98         if (temp != priQue.top())
 99         {
100             vec.push_back(temp);
101             if (vec.size() > 5842)
102                 return;
103             priQue.push(temp * 2);
104             priQue.push(temp * 3);
105             priQue.push(temp * 5);
106             priQue.push(temp * 7);
107         }
108     }
109 }
110 
111 #endif
View Code

 

posted on 2019-05-23 17:08  jephsdge  阅读(318)  评论(0)    收藏  举报