1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
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;
}