-
题目描述:
-
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。
习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
-
输入:
-
输入包括一个整数N(1<=N<=1500)。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出第N个丑数。
-
样例输入:
-
3
-
样例输出:
-
3
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.lang.Comparable;
import java.util.ArrayList;
import java.util.PriorityQueue;
class Main
{
public static final boolean DEBUG = false;
public static ArrayList<Long> al = new ArrayList<Long>();
static class Pair implements Comparable<Pair>
{
long first;
int second;
public Pair(long f, int s)
{
first = f;
second = s;
}
public int compareTo(Pair other)
{
if (first != other.first) {
if (first < other.first) return -1;
else return 1;
}
return second - other.second;
}
}
public static void init()
{
Pair p = new Pair(1, 2);
PriorityQueue<Pair> pq = new PriorityQueue<Pair>();
pq.add(p);
for (int i = 0; i < 1501; i++) {
Pair tmp = pq.poll();
al.add(tmp.first);
switch(tmp.second) {
case 2:
pq.add(new Pair(tmp.first * 2, 2));
case 3:
pq.add(new Pair(tmp.first * 3, 3));
case 5:
pq.add(new Pair(tmp.first * 5, 5));
}
}
}
public static void main(String[] args) throws IOException
{
Scanner cin;
int n;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(new InputStreamReader(System.in));
}
init();
while (cin.hasNext()) {
n = cin.nextInt();
System.out.println(al.get(n - 1));
}
}
}