题目大意:
输出第n个(素数因子只能为2、3、5、7(或者关系))的合数(这个是引用自http://blog.csdn.net/kg_second/article/details/7922054 muximuxi@Achilles的专栏 )
//原本我的思路是暴搜,可惜会超时,后来我可耻的看了答案,发现一个巧妙的方法,虽然不知道别人是怎么想出来的,但是可行就好,不要过于纠结
//在我自己写的过程中,我终于理解了
#include<stdio.h>
#include<string.h>
int minn(int a,int b,int c,int d)
{
int m;
m=b<a? b:a;
m=m<c? m:c;
m=m<d? m:d;
return m;
}
int main()
{
int n,i,f[6000],mi,a=1,b=1,c=1,d=1;
f[1]=f[2]=f[3]=f[4]=1;
for(i=2;i<=5842;i++)//一个个找
{
mi=minn(2*f[a],3*f[b],5*f[c],7*f[d]);//注意是a和f[a],b和f[b],c和f[c],d和f[d],这才是理解的关键
if(mi==2*f[a])a++;//这样子
if(mi==3*f[b])b++;//果然就不会
if(mi==5*f[c])c++;//漏掉
if(mi==7*f[d])d++;//任何一个了
f[i]=mi;
}
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
if(n%100!=11&&n%10==1)
printf("The %dst humble number is %d.\n",n,f[n]);
else if(n%100!=12&&n%10==2)
printf("The %dnd humble number is %d.\n",n,f[n]);
else if(n%100!=13&&n%10==3)
printf("The %drd humble number is %d.\n",n,f[n]);
else
printf("The %dth humble number is %d.\n",n,f[n]);
}
return 0;
}