hdu 1058 Humble Numbers
状态转移方程:F(n)=min(F(i)*2,F(j)*3,F(k)*5,F(m)*7) (n>i,j,k,m)
特别的:i,j,k,m 只有在本项被选中后才移动
题目还要注意的一点是输出问题。。。
1 #include<stdio.h>
2 int hum[6000];
3
4 int min(int a, int b, int c, int d)
5 {
6 int m;
7 if(a>b) m = b;
8 else m = a;
9 if(m>c) m =c;
10 if(m>d) m =d;
11 return m;
12 }
13 int main()
14 {
15 int e1=1,e2=1,e3=1,e4=1;
16 int a1,a2,a3,a4,i,n;
17 hum[1] = 1;
18 for(i=2;i<5843;i++)
19 {
20 a1 = 2*hum[e1];
21 a2 = 3*hum[e2];
22 a3 = 5*hum[e3];
23 a4 = 7*hum[e4];
24 hum[i] = min(a1,a2,a3,a4);
25 if(hum[i] == a1) e1++;
26 if(hum[i] == a2) e2++;
27 if(hum[i] == a3) e3++;
28 if(hum[i]==a4) e4++;
29 }
30 while(scanf("%d",&n),n)
31 {
32 if(n%100==11||n%100==12||n%100==13)
33 printf("The %dth humble number is %d.\n",n,hum[n]);
34 else if(n%10 == 1) printf("The %dst humble number is %d.\n",n,hum[n]);
35 else if(n%10 == 2) printf("The %dnd humble number is %d.\n",n,hum[n]);
36 else if(n%10 == 3) printf("The %drd humble number is %d.\n",n,hum[n]);
37 else printf("The %dth humble number is %d.\n",n,hum[n]);
38 }
39 return 0;
40 }
参考:http://www.wutianqi.com/?p=951