随笔 - 250  文章 - 3  评论 - 66  阅读 - 21万

HDU1058Humble Numbers(DP)

转到题目。

解题分析:

既然让求因子只能为2357的数, 那么分别看2357的倍数就OK了。

另外。输出时要注意st(first), nd(secend), rd(third)

因为11,12,13时为th 所以111和101也是不同的

代码如下:

复制代码
#include <stdio.h>

#define n 5900

int hum[n];

int min_num(int a, int b, int c, int d){
    int t = a;
    if(t>b) t = b;
    if(t>c) t = c;
    if(t>d) t = d;
    return t;
}

int main(){
    int a1, a2, a3, a4, b1, b2, b3, b4, m;
    int i;
    hum[1] = 1;
    a1 = a2 = a3 = a4 = 1;
    for(i=2; i<=n; i++){
        b1 = hum[a1] * 2;
        b2 = hum[a2] * 3;
        b3 = hum[a3] * 5;
        b4 = hum[a4] * 7;
        hum[i] = min_num(b1, b2, b3, b4);
        if(b1 == hum[i]) a1++;
        if(b2 == hum[i]) a2++;
        if(b3 == hum[i]) a3++;
        if(b4 == hum[i]) a4++;
    }

    while(scanf("%d", &m) == 1 && m != 0){
        if(m % 10 == 1 && m % 100 != 11) printf("The %dst humble number is %d.\n", m, hum[m]);
        else if(m % 10 == 2 && m % 100 != 12) printf("The %dnd humble number is %d.\n", m, hum[m]);
        else if(m % 10 == 3 && m % 100 != 13) printf("The %drd humble number is %d.\n", m, hum[m]);
        else printf("The %dth humble number is %d.\n", m, hum[m]);
    }

    return 0;
}
复制代码

 

posted on   Still_Raining  阅读(251)  评论(0编辑  收藏  举报
< 2013年2月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示