题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058
代码如下:
#include<stdio.h>//hdu1058 丑数
#define min(a,b) (a<b?a:b)
#define min4(a,b,c,d) min(min(a,b),min(c,d))
int main()
{
int a[5900]={0,1},n = 1,t,p2,p3,p5,p7;
p2 = p3 = p5 = p7 = 1;
while(n<5842)
{
a[++n] = min4(2*a[p2],3*a[p3],5*a[p5],7*a[p7]);
if(a[n]==2*a[p2]) p2++;
if(a[n]==3*a[p3]) p3++;
if(a[n]==5*a[p5]) p5++;
if(a[n]==7*a[p7]) p7++;
}
while(scanf("%d",&t) && t)
{
if(t%10==1 && t%100!=11)
printf("The %dst humble number is %d.\n",t,a[t]);
else if(t%10==2 && t%100!=12)
printf("The %dnd humble number is %d.\n",t,a[t]);
else if(t%10==3 && t%100!=13)
printf("The %drd humble number is %d.\n",t,a[t]);
else
printf("The %dth humble number is %d.\n",t,a[t]);
}
return 0;
}