HDU 1058 humble number

不用指针优化就703MS  指针优化93MS

注意要用int64 因为过程中可能会相乘溢出

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>

const int INF = 2100000000 ;
int dir[5] = {0,2, 3, 5, 7};
int p[5] = {0,1, 1, 1, 1};
__int64 table[6000];
int main()
{
    int cnt = 1, i, j, n, pos;
    __int64 min ;
    table[1] = 1;
    while(cnt <= 5843)
    {
        min = INF ;        
        for(j=1; j<=4; j++)
        {
            __int64 tmp = dir[j] * table[p[j]];
            if( tmp > table[cnt] && tmp < min )
            {
                min = tmp;
    //            pos = j;
            }
        }        
        table[++cnt] = min ;
//        p[pos] ++ ; 错的。这样只加了其中一种 如table[cnt] = 6 时 有可能 2*3 和 3*2 所以要都加,如下
        for(j=1; j<=4; j++)
            if(table[cnt] == dir[j]*table[p[j]])
                p[j]++;
    }
    while(scanf("%d", &n), n!=0)
    {
        if(n%100 ==11 || n%100 == 12 || n%100 == 13)
            printf("The %dth humble number is %d.\n", n, table[n]);
        else if( n%10 == 1)
            printf("The %dst humble number is %d.\n", n,table[n]);
        else if( n%10 == 2)
            printf("The %dnd humble number is %d.\n", n,table[n]);
        else if( n%10 == 3)
            printf("The %drd humble number is %d.\n", n,table[n]);
        else
            printf("The %dth humble number is %d.\n", n,table[n]);
        
    }

    return 0;
}

 

posted @ 2013-03-08 16:47  April_Tsui  阅读(148)  评论(0编辑  收藏  举报