題目:求由{2,3,5,7}組成那的數字集合的第n個數字。
分析:利用4根指針,對應每個數字當前對應的因子,每次取最小的數值,指針後移。注意相等的情況。
注意:輸出的計數法。
[cpp]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int d[ 5850 ];
int v[4] = {2,3,5,7};
int p[4] = {1,1,1,1};
int main()
{ www.2cto.com
int count = 2; d[1] = 1;
while ( count <= 5842 ) {
int min = 2000000001;
for ( int i = 0 ; i < 4 ; ++ i )
if ( min > v[i]*d[p[i]] )
min = v[i]*d[p[i]];
for ( int i = 0 ; i < 4 ; ++ i )
if ( min == v[i]*d[p[i]] )
p[i] ++;
d[count ++] = min;
}
int n;
while ( scanf("%d",&n) && n ) {
printf("The %d",n);
if ( n%100 == 11 || n%100 == 12 || n%100 == 13 )
printf("th");
else if ( n%10 == 1 )
printf("st");
else if ( n%10 == 2 )
printf("nd");
else if ( n%10 == 3 )
printf("rd");
else printf("th");
printf(" humble number is %d.\n",d[n]);
}
return 0;
}