hdu1058

杭电2010和2011级同学如何加入ACM集训队?

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7113    Accepted Submission(s): 3105


Problem Description
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence
 

Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
 

Output
For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.
 

Sample Input
   
   
   
   
1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
 

Sample Output
   
   
   
   
The 1st humble number is 1. The 2nd humble number is 2. The 3rd humble number is 3. The 4th humble number is 4. The 11th humble number is 12. The 12th humble number is 14. The 13th humble number is 15. The 21st humble number is 28. The 22nd humble number is 30. The 23rd humble number is 32. The 100th humble number is 450. The 1000th humble number is 385875. The 5842nd humble number is 2000000000.
 


/*整体思想是利用已经知道的序列中的元素根据规则去生成新的元素, 如x * 2, x * 3, x * 5, x * 7.


假设使用数组a[MAX]进行存储这一序列的所有元素, 首先使a[0] = 1, 表示第一个元素是1, 然后利用4个指针
(不是内存指针,呵呵), i2 = i3 = i5 = i7 = 0. 每次我们比较 a[i2] * 2, a[i3] * 3, a[i5] * 5, a[i7] * 
 7 这四个元素的大小, 把最小的放到序列中, 并把对应的指针+1, 直到生成需要的个数.
*/
 


#include<stdio.h>  


#include<string.h>  


#define min(e,f) (e)>(f)?(f):(e)//好方法  


int dp[6000] = { 0, 1 },i2 = 1, i3 = 1, i5 = 1, i7 = 1, cnt = 1, N;  


char lst[5][5] = { "th", "st", "nd", "rd" };  


int getUp () {  


    int t = min ( min ( 2*dp[i2], 3*dp[i3] ),   


                  min ( 5*dp[i5], 7*dp[i7] ) );    


    if ( t == 2*dp[i2] ) ++ i2;  


    if ( t == 3*dp[i3] ) ++ i3;  


    if ( t == 5*dp[i5] ) ++ i5;  


    if ( t == 7*dp[i7] ) ++ i7;   


    return t;   


}  


void init () 
{  


    for ( int i = 2; i <= 5842; ++ i ) 
{  


        dp[i] = getUp ();  


    }       


}  


char * getLst ( int n ) {  


     if ( n % 100 != 11 && n % 10 == 1 ) return lst[1];  


     else if ( n % 100 != 12 && n % 10 == 2 ) return lst[2];  


     else if ( n % 100 != 13 && n % 10 == 3 ) return lst[3];  


     else return lst[0];        


}  


int main ()  


{  


    init();  


    while ( scanf("%d",&N)!=EOF &&N)  


    {  


          printf ( "The %d", N );  


          printf ( "%s", getLst ( N ) );  


          printf ( " humble number is %d.\n", dp[N] );        


    }  


    return 0;  





 

你可能感兴趣的:(Integer,存储,input,each,2010)