//重在理解方法:每次找到最小的 2 3 5 7 的因子数,之后利用所存数的下标的关系改变
#include <iostream>
using namespace std;
int num[5843]; //存储前5842个丑数
int find_min ( int a, int b, int c, int d )
{
int temp = a < b ? a : b;
int index = c < d ? c : d;
return temp < index ? temp : index;
}
void solve ( )
{
int i1, i2, i3, i4, i;
int h1, h2, h3, h4;
i1 = i2 = i3 = i4 = 1;
num[1] = 1;
for (i = 2; i < 5843; i ++ )
{
h1 = num[i1] * 2;
h2 = num[i2] * 3;
h3 = num[i3] * 5;
h4 = num[i4] * 7;
int min = find_min ( h1, h2, h3, h4 );
num[i] = min;
//易错点:这里不可以用else if 因为ti中可能会有相同的最小值,如当:min = 6 时
if ( min == h1 )
i1 ++;
if ( min == h2 )
i2 ++;
if ( min == h3 )
i3 ++;
if ( min == h4 )
i4 ++;
}
}
int main ()
{
solve ();
int n;
while ( scanf ("%d", &n), n )
{
if ( n % 100 != 11 && n % 10 == 1 )
printf ("The %dst humble number is %d.\n", n, num[n]);
else if ( n % 100 != 12 && n % 10 == 2 )
printf ("The %dnd humble number is %d.\n", n, num[n]);
else if ( n % 100 != 13 && n % 10 == 3 )
printf ("The %drd humble number is %d.\n", n, num[n]);
else
printf ("The %dth humble number is %d.\n", n, num[n]);
}
//system ("pause");
return 0;
}
posted on 2010-09-11 20:56
雪黛依梦 阅读(561)
评论(0) 编辑 收藏 引用 所属分类:
简单题 、
数学题