http://acm.hdu.edu.cn/showproblem.php?pid=1058
题意:求出前5842个质因数只在(2,3,5,7)中的数.
思路:感觉有点像递推。若一个数是Humble Numbers,那么它的2,3,5,7倍也是Humble Numbers。但是要求数组是按下标递增的,所以 f[n] = min(2*f[i], 3*f[j], 5*f[k], 7*f[l]),i, j,k,l在选择后进行右移。做了这道题还学会了英语计数,st,nd,rd,th终于搞明白了.....
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int f[5845]; int main() { f[1] = 1; int i = 1,j = 1,k = 1,l = 1; for(int n = 2; n <= 5842; n++) { int ans = min( min(2*f[i], 3*f[j]),min(5*f[k], 7*f[l]) ); if(ans == 2*f[i]) i++; if(ans == 3*f[j]) j++; if(ans == 5*f[k]) k++; if(ans == 7*f[l]) l++; f[n] = ans; } int m; while(~scanf("%d",&m)&& m) { if(m%10 == 1 && m%100 != 11) printf("The %dst humble number is %d.\n",m,f[m]); else if(m%10 == 2 && m%100 != 12) printf("The %dnd humble number is %d.\n",m,f[m]); else if(m%10 == 3 && m%100 != 13) printf("The %drd humble number is %d.\n",m,f[m]); else printf("The %dth humble number is %d.\n",m,f[m]); } return 0; }