zoj 1095 Humble Numbers

// code by wshong
#include < stdio.h >
#include
< stdlib.h >
int  main()
{
    
int ham[5842];
    
int a[4]={0,0,0,0};
    ham[
0= 1;
    
int i;
    
for(i = 1; i < 5842; i++)
    
{
        
int tem1 = 2 * ham[a[0]] > 3* ham[a[1]] ? 3*ham[a[1]]: 2 * ham[a[0]];
        
int tem2 = 5 * ham[a[2]] > 7 * ham[a[3]]? 7*ham[a[3]]:5 *ham[a[2]];
        
int temp = tem1 > tem2 ? tem2:tem1;
        ham[i] 
= temp;
        
if(ham[i] == 2 * ham[a[0]]) a[0]++;
        
if(ham[i] == 3 * ham[a[1]]) a[1]++;
         
if(ham[i] == 5 * ham[a[2]]) a[2]++;
         
if(ham[i] == 7 * ham[a[3]]) a[3++;     
          
    }

    
for(i = 0; i < 5842; i++)
    printf(
"%d,",ham[i]);
    
int n;
    
while(scanf("%d",&n),n)
    
{
           
if(n % 10 == 1 &&%100!=11)
           printf(
"The %dst humble number is %d. ",n,ham[n-1]);
           
else if(n % 10 == 2 && n %100 != 12 )
           printf(
"The %dnd humble number is %d. ",n,ham[n-1]);
           
else if(n%10 == 3 && n %100!= 13)
           printf(
"The %drd humble number is %d. ",n,ham[n -1]);
           
else printf("The %dth humble number is %d. ",n,ham[n -1 ]);                
    }

    system(
"pause");
    
return 0;
    }

/*参照了别人的程序,这道题的难点在与怎么确定比现在数大的最小数,题目用的很巧,这种方法很好用!
之前老是超时,要先打表,然后直接就可以读出相应的数的了,算法也很容易明白
*/
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值