题意:
定义Humble Number 为素因子只为 2,3,5,7,求第i个HN
思路:
递推思维,每一个HN必定是由前面某一个HN乘上2.3.5.7的某一个数得到的,假设希望求第n个HN,DP[N] , 那么我们就要在前N-1个里面找到乘上2.3.5.7中的一个,满足刚好大于DP[n-1] 且最小。我们留标记位one , three , five ,sevent 保证这四个标记为都有机会从1 扫到n-1, 用过一次就向后推一次 。
st、nd、rd的笔记:
单独看最后的两位,若属于10-19,那么全是th 。
其他的区间,最低位 是1 那么st,2 -> nd, 3->rd 。其他的th
代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5845 ;
long long dp[maxn];
void init()
{
int two = 1, three = 1, five = 1, sevent = 1;
dp[1] = 1;
int i=2;
while(i<=5842)
{
dp[i] = min( min(dp[two]*2, dp[three]*3), min(dp[five]*5, 7*dp[sevent]));
//cout<<dp[i]<<endl;
if(dp[two]*2 == dp[i])
two++;
if(dp[three]*3 == dp[i])
three++;
if(dp[five]*5 == dp[i])
five++;
if(dp[sevent]*7 == dp[i])
sevent++;
i++;
}
}
int main()
{
init();
int n,cnt;
while(scanf("%d",&cnt) && cnt )
{
char ch[4] = "th";
n = cnt%100;
if(n>20 || n<10)
{
if(n%10 == 1)
strcpy(ch,"st");
else if(n%10 ==2)
strcpy(ch,"nd");
else if(n%10 == 3)
strcpy(ch,"rd");
}
printf("The %d%s humble number is %lld.\n",cnt,ch,dp[cnt]);
}
}