hdu 1058 Humble Numbers(DP)

题意:一组只因子只含有2,3,5,7的数据从小到大排列,求出第n个数
解题思路:1.此题为动态规划,难点在于如何将数据从小到大放在数组中
2.各个因子逐渐增加相乘,再比较大小得出数组

3.注意输出格式,除了11,12,13例外,其余格式余数1为st,2为nd,3为rd,其余为th

4.如果一个数是Humble Number,那么它的2倍,3倍,5倍,7倍仍然是Humble Number
定义F[i]为第i个Humble Number
F[n]
定义F[i]为第i个Humble Number
F[n]=min(2*f[i],3*f[j],5*f[k],7*f[L]), i,j,k,L在被选择后相互移动

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[6000]={0};
int Min(int a,int b,int c,int d){
	return min(a,min(b,min(c,d)));
}
void init(){
	dp[1]=1;
	int h1=1,h2=1,h3=1,h4=1;
	for(int i=2;i<=5842;i++){
		dp[i]=Min(dp[h1]*2,dp[h2]*3,dp[h3]*5,dp[h4]*7);
		if(dp[i]==dp[h1]*2) h1++;
		if(dp[i]==dp[h2]*3) h2++;
		if(dp[i]==dp[h3]*5) h3++;
		if(dp[i]==dp[h4]*7) h4++;
	}
}
int main(){
	int n;
	init();
	while(scanf("%d",&n),n){
		if(n%10==1&&n%100!=11){
			printf("The %dst humble number is %d.",n,dp[n]);
		}else if(n%10==2&&n%100!=12){
			printf("The %dnd humble number is %d.",n,dp[n]);
		}else if(n%10==3&&n%100!=13){
			printf("The %drd humble number is %d.",n,dp[n]);
		}else printf("The %dth humble number is %d.",n,dp[n]);
		printf("\n");
	}
	return 0;
}



你可能感兴趣的:(hdu 1058 Humble Numbers(DP))