传送门: POJ1338
关键词: 枚举;队列;
这题目还是有很多值得品位的地方,思路上其实不是很容易想到。
Description
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, …
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n’th ugly number.
丑陋数(Ugly Number)是仅含有素因子2、3或5的整数。序列给出了前10个丑陋数。按照惯例,1被包含在丑陋数中。给出整数n,编写一个程序找出并打印第n个丑陋数。
Input
Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.
输入的每行给出一个正整数n(n<=1500)。输入以n=0的一行结束。
Output
For each line, output the n’th ugly number .:Don’t deal with the line with n=0.
对于输入的每一行,输出第n个丑陋数,对n=0的那一行不处理。
Sample Input
1
2
9
0
Sample Output
1
2
10
Source
New Zealand 1990 Division I,UVA 136
解析
- n <= 1500,可以采用离线求解的形式,先把前1500个ugly number计算并保存,之后需要那个,直接下标索引就可以了。
- 为了让生成的ugly number有序,每次选择最小的整数来递进(
threeMin
)。 - P.S. 实际上可以理解为三个队列。