POJ2247总结

Humble Numbers

题目来源

Description

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, … shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence.

Input

The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line saying “The nth humble number is number.”. Depending on the value of n, the correct suffix “st”, “nd”, “rd”, or “th” for the ordinal number nth has to be used like it is shown in the sample output.

Sample Input

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

Sample Output

The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

我的代码

暴力出5842个Humble Numbers打入优先队列中,按照升序排序。注意剪枝就行。

#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;

priority_queue < int , vector<int>, greater<int> > q;
int ans[6000];
int main()
{
	int n;
	long long two=1,three=1,five=1,seven=1;
	for (int s=1;s<=12;s++)
	{
		long long five=1;
		for (int f=1;f<=14;f++)
		{
			if (five*seven>2000000000) break;
			long long three=1;
			for (int th=1;th<=20;th++)
			{
				if (five*seven*three>2000000000) break;
				long long two=1;
				for (int tw=1;tw<=31;tw++)
				{
					if (five*seven*three*two>2000000000) break;
					else q.push(five*seven*three*two);
					two=two*2;
				}
				three=three*3;
			}
			five=five*5;
		}
		seven=seven*7;
	}
	for (int i=1;i<=5842;i++)
	{
		int t=q.top();
		q.pop();
		ans[i]=t;
	}
	scanf("%d",&n);
	while(n!=0)
	{
		
		if(n%10==1&&n%100!=11)
        	printf("The %dst humble number is %d.\n",n,ans[n]);
    	else if(n%10==2&&n%100!=12)
        	printf("The %dnd humble number is %d.\n",n,ans[n]);
    	else if(n%10==3&&n%100!=13)
        	printf("The %drd humble number is %d.\n",n,ans[n]);
    	else
        	printf("The %dth humble number is %d.\n",n,ans[n]);
		scanf("%d",&n);
	}
	return 0;
}

反思

我真傻真的我单知道11、12、13的单词后缀和普通的一样是th;我不知道只要mod 100 ==11的都是就这样WA了一次。

但是我搜了一下翻译,怎么感觉翻译不太对劲?

另外优先队列的两种升序排序方法(没用结构体)

priority_queue < int , vector<int>, greater<int> > q; //从小到大,需要vector
priority_queue < int , vector<int> , cmp  > q;//从小到大,需要vector

来源

发表评论