HDU-1058Humble Numbers

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058

本题思路很简单:就是用2,3,5,7循环来 
求第i个f[i],第i个f[i]必定等于 
前i-1个数中其中一个数与{2,3,5,7}中 
其中一个的乘积,于是答案就出来了,就是取从1开始取与2,3,5,7相乘,取最小的,取了一个对应指针就往前移一位

#include<cstdio>
#include<iostream>
#include<cstring>
#define N 5843
using namespace std;
int dp[N];
int min(int x,int y) {
	return x<y?x:y;
}
int main() {
	int n,a,b,c,d;
	a=b=c=d=1;
	dp[1]=1;
	for(int i=2; i<=N-1; i++) {
		dp[i]=min(dp[a]*2,min(dp[b]*3,min(dp[c]*5,dp[d]*7)));
		if(dp[i]==dp[a]*2) a++;
		if(dp[i]==dp[b]*3) b++;
		if(dp[i]==dp[c]*5) c++;
		if(dp[i]==dp[d]*7) d++;
	}
	while(~scanf("%d",&n)&&n) {
		printf("The %d",n);
		if(n%10==1&&n%100!=11) printf("st ");
		else if(n%10==2&&n%100!=12) printf("nd ");
		else if(n%10==3&&n%100!=13) printf("rd ");
		else   printf("th ");
		printf("humble number is %d.\n",dp[n]);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39564498/article/details/81711251