杭电acm1058
http://acm.hdu.edu.cn/showproblem.php?pid=1058
见注释
View Code
1 #include<stdio.h>//解法同uva里的136题,丑数,要先打表,然后再输出 2 int min(int a,int b) 3 { 4 if(a>=b) 5 return b; 6 else return a; 7 } 8 int main() 9 { 10 int a[5843],b[4]; 11 int i,j,temp,a1,a2,a3,n; 12 a[1]=1; 13 for(i=1;i<=5842;) 14 { 15 for(j=0;j<=i,a[j]*2<=a[i];j++); 16 b[0]=a[j]*2; 17 for( j=0;j<=i,a[j]*3<=a[i];j++); 18 b[1]=a[j]*3; 19 for( j=0;j<=i,a[j]*5<=a[i];j++); 20 b[2]=a[j]*5; 21 for( j=0;j<=i,a[j]*7<=a[i];j++); 22 b[3]=a[j]*7; 23 a[i+1]=min(b[3],min(b[0],min(b[1],b[2]))); 24 i++;//以1为源头,逐个乘上2.3.5.7,然后取最小的,存到数组里面 25 } 26 while(scanf("%d",&n)&&n) 27 { 28 if(n%100==11) 29 printf("The %dth humble number is %d.\n",n,a[n]); 30 else if(n%100==12) 31 printf("The %dth humble number is %d.\n",n,a[n]); 32 else if(n%100==13) 33 printf("The %dth humble number is %d.\n",n,a[n]);//尾数是11.12.13的都不必特意输出,这个……是英语范畴的,不大理解 34 else if(n%10==1) 35 printf("The %dst humble number is %d.\n",n,a[n]); 36 else if(n%10==2) 37 printf("The %dnd humble number is %d.\n",n,a[n]); 38 else if(n%10==3) 39 printf("The %drd humble number is %d.\n",n,a[n]);//如果尾数是1.2.3要特别输出 40 else printf("The %dth humble number is %d.\n",n,a[n]);//否则直接输出 41 } 42 return 0; 43 }