UVa 443 - Humble Numbers
题目:求由{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.zzzyk.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;
}
补充:软件开发 , C++ ,