hdu 1058 Humble Numbers(dp)

看的题解。。。不知道这些方法都是怎么想出来的,好厉害啊,估计我是想不出来的,用b2,b3,b5,b7来分别记录

从第几位数开始还没有乘对应的2,3,5,7然后比较,小的数排上就ok了。。。注意有的时候是、会出现乘积相同,

所以都得加一,比如2*3和3*2,都得让b2,b3加一。这道题还涉及到了英语上的知识,就是只有11,12,13是序数

词时加th,1,2,3,分别是first,second,third。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<string>
#include<algorithm>

using namespace std;

int main()
{
	int a[5850];
	int temp,n;
	a[1] = 1;
	int b2,b3,b5,b7;
	b2 = b3 = b5 = b7 = 1;
	int m = 2;
	while(m<5843)
	{
		temp = min(2*a[b2],min(3*a[b3],min(5*a[b5],7*a[b7])));
		a[m++] = temp;
		if(temp == 2*a[b2])		b2++;
		if(temp == 3*a[b3])	b3++;
		if(temp == 5*a[b5])	b5++;
		if(temp == 7*a[b7])	b7++;
	} 
	while(cin >> n,n)
	{
		int x = a[n];
		//printf("The 1st humble number is 1.")
		if(n%100==11 || n%100 == 12 || n%100 == 13)
			printf("The %dth humble number is %d.\n",n,x);
		else if(n%10 == 1)
			printf("The %dst humble number is %d.\n",n,x);
		else if(n%10 == 2)
			printf("The %dnd humble number is %d.\n",n,x);
		else if(n%10 == 3)
			printf("The %drd humble number is %d.\n",n,x);
		else
			printf("The %dth humble number is %d.\n",n,x);
	}
	return 0;
}


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