hdoj 1058 Humble Numbers【dp】

#include <stdio.h>
int pos[4] = {1, 1, 1, 1};
const int flag[4] = {2, 3, 5, 7};
int  mul[4] = {2, 3, 5, 7};
int hum[5845] = {0, 1, 2};
inline int getMin()//每次加入数列的数显然都是2,3,5或7乘以一个数,这个数必然要从数列中取,然后取最小值。
{
	int min = 2000000001;
	for(int i=0; i<4; i++)
		if(min > mul[i])
			min = mul[i];
	for(int i=0; i<4; i++)
		if(min == mul[i])
		{
			pos[i]++;
			mul[i] = hum[pos[i]] * flag[i];
		}
	return min;
}

int main()
{
	for(int i=2; i<=5842; i++)
		hum[i] =getMin();
	int pos;
	while(scanf("%d", &pos), pos)
	{
		if(pos%10 == 1 && pos%100 != 11)
			printf("The %dst humble number is ", pos);
		else if(pos%10 == 2 && pos%100 != 12)
			printf("The %dnd humble number is ", pos);
		else if(pos%10 == 3 && pos%100 != 13)
			printf("The %drd humble number is ", pos);
		else
			printf("The %dth humble number is ", pos);
		printf("%d.\n", hum[pos]);
	}
	return 0;
}




你可能感兴趣的:(hdoj 1058 Humble Numbers【dp】)