php 网站建设福州seo排名优化公司
一个素数只有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)终止。
输出规格
对于每个测试用例,打印一行,上面写着“第n个谦逊的数字是数字。”。根据n的值,必须使用序数第n的正确后缀“st”、“nd”、“rd”或“th”,就像示例输出中所示的那样。
Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
Sample Output
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.
思路
英语语法题,除凡是11、12、13结尾的数字接th外,凡是1、2、3结尾的数字分别接st、nd、rd,其他数字接th。
AC代码
#include <iostream>
#define AUTHOR "HEX9CF"
using namespace std;const int maxn = 5843;long long a[maxn];
int t2, t3, t5, t7;int main()
{int n;t2 = t3 = t5 = t7 = 1;a[1] = 1;for (int i = 2; i < maxn; i++){a[i] = min(min(2 * a[t2], 3 * a[t3]), min(5 * a[t5], 7 * a[t7]));if (a[i] == 2 * a[t2]){t2++;}if (a[i] == 3 * a[t3]){t3++;}if (a[i] == 5 * a[t5]){t5++;}if (a[i] == 7 * a[t7]){t7++;}}while (cin >> n && n){int h = n % 100;if (h > 10 && h < 14){printf("The %dth humble number is %d.\n", n, a[n]);}else{switch (n % 10){case 1:printf("The %dst humble number is %d.\n", n, a[n]);break;case 2:printf("The %dnd humble number is %d.\n", n, a[n]);break;case 3:printf("The %drd humble number is %d.\n", n, a[n]);break;default:printf("The %dth humble number is %d.\n", n, a[n]);break;}}}return 0;
}