一.题目链接:
HDU-1058
二.题目大意:
规定因子(1 和 本身除外)只有 2, 3,5,7 的数为 Humble Number.
求第 n 个 Humble Number 的值.
三.分析:
题目给出:a[1] == 1.
然后求 2,3,5,7 的倍数,取最小的数,再让它的倍数++;
四.代码实现:
#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-4
#define PI acos(-1.0)
#define ll long long int
using namespace std;
int a[6000];
void init()
{
int x2, x3, x5, x7;
x2 = x3 = x5 = x7 = 1;
a[1] = 1;
for(int i = 2; i < 5843; ++i)
{
a[i] = min(min(a[x2] * 2, a[x3] * 3), min(a[x5] * 5, a[x7] * 7));
if(a[i] == a[x2] * 2)
x2++;
if(a[i] == a[x3] * 3)///注意这里不能是else if,因为两者可能相等.
x3++;
if(a[i] == a[x5] * 5)
x5++;
if(a[i] == a[x7] * 7)
x7++;
}
}
int main ()
{
init();
int n;
while(scanf("%d", &n) && n)
{
if(n % 100 == 11 || n % 100 == 12 || n % 100 == 13)///蒟蒻格式盲区
printf("The %dth humble number is %d.\n", n, a[n]);
else if(n % 10 == 1)
printf("The %dst humble number is %d.\n", n, a[n]);
else if(n % 10 == 2)
printf("The %dnd humble number is %d.\n", n, a[n]);
else if(n % 10 == 3)
printf("The %drd humble number is %d.\n", n, a[n]);
else
printf("The %dth humble number is %d.\n", n, a[n]);
}
return 0;
}