POJ2247,hdu1058(Humble Numbers)
Problem Description
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.
Write a program to find and print the nth element in this sequence
Write a program to find and print the nth element in this sequence
Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.
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.题意:寻找第n个humble number(百度翻译貌似是谦虚的数。。。什么破玩意儿?!!?!),humble number的定义为一个质因数为2或3或5或7的整数而且1也包括在内。当然比较坑的地方就是输出格式,关于输出st,nd,rd这几个单词的时候要注意,最后两位是十几的情况下只能用th,因为11是eleventh,12是twelvth,13是thirth(好吧,貌似写错别字了,估计四级又要不过)。思路:看我之前的ugly number的题解。。。 http://blog.csdn.net/ecjtuacm_yuewei/article/details/42365475(好吧,任性了),其实这两道题目很相似的。。。懂?!代码://hdu1058 #include<iostream> #include<cstdio> #include<algorithm> #include<cstdio> #include<cmath> #define maxn 6000 using namespace std; __int64 biao[maxn]; inline __int64 min(__int64 x, __int64 y) { return x < y?x:y; } int main() { int er = 1, san = 1, wu = 1, qi = 1; biao[1] = 1; for(int i = 2; i <= 5845; i ++) { biao[i] = min(min(biao[er] * 2, biao[san] * 3), min(biao[wu] * 5, biao[qi] * 7)); if(biao[i] == biao[er] * 2) er ++; if(biao[i] == biao[san] * 3) san ++; if(biao[i] == biao[wu] * 5) wu ++; if(biao[i] == biao[qi] * 7) qi ++; } // for(int i = 1; i <= 20; i ++) // cout << biao[i] << ' '; int n; while(cin >> n && n) { int k = n % 10; int w = n / 10 % 10; if(k == 1 && w != 1) { printf("The %dst humble number is %I64d.\n", n, biao[n]); } else if(k == 2 && w != 1) { printf("The %dnd humble number is %I64d.\n", n, biao[n]); } else if(k == 3 && w != 1) { printf("The %drd humble number is %I64d.\n", n, biao[n]); } else { printf("The %dth humble number is %I64d.\n", n, biao[n]); } } return 0; }
本文来自博客园,作者:MrYu4,转载请注明原文链接:https://www.cnblogs.com/MrYU4/p/15778910.html