原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1058
题目大意:
若一个数的所有素因子是2,3,5,7中的一个或多个,则称该数为Humble Numbers
求第n个Humble Numbers。
分析:
Humble Numbers的2,3,5,7倍也是Humble Numbers。
注意处理过程中相同的数。
代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 5900;
int hp[N];
int min(int a, int b, int c, int d)
{
if (a <= b&&a <= c&&a <= d)return a;
if (b <= c&&b <= d) return b;
if (c <= d) return c;
return d;
}
int main()
{
int k2 = 1, k3 = 1, k5 = 1, k7 = 1;//分别代表2,3,5,7光标指向数组的位置
int x;
hp[1] = 1;
for (int i = 2; i <= 5842; i++)
{
x = min(hp[k2] * 2, hp[k3] * 3, hp[k5] * 5, hp[k7] * 7);
hp[i] = x;
if (x == hp[k2] * 2)k2++; //注意不能加else 因为这样可以处理相同的数
if (x == hp[k3] * 3)k3++;
if (x == hp[k5] * 5)k5++;
if (x == hp[k7] * 7)k7++;
}
int n;
while (cin >> n&&n)
{
cout << "The " << n;
if (n % 10 == 1 && n % 100 != 11)
cout << "st";
else if (n % 10 == 2 && n % 100 != 12)
cout << "nd";
else if (n % 10 == 3 && n % 100 != 13)
cout <<"rd";
else cout << "th";
cout <<" humble number is "<< hp[n] <<"."<< endl;
}
return 0;
}