🌕🌑🌑🌑🌑
# 題目連結
- 題目連結
- Online Judge
- uDebug
# 題目說明
Time limit: 3.000 seconds
# 題目
Ugly numbers are numbers whose only prime factors are 2
, 3
or 5
. The sequence
shows the first 11
ugly numbers. By convention, 1
is included.
Write a program to find and print the 1500’th
ugly number.
# Input
There is no input to this program.
# Output
Output should consist of a single line as shown below, with ‘<number>’
replaced by the number computed.
# Sample Output
The 1500'th ugly number is <number>.
# 解題技巧
# Solution
public class Main { | |
public static void main(String[] args) { | |
int[] uglyNum = new int[1505]; | |
int index2 = 1, index3 = 1, index5 = 1; | |
uglyNum[1] = 1; | |
for (int i = 2; i <= 1500; i++) { | |
while (uglyNum[index2] * 2 <= uglyNum[i - 1]) | |
index2++; | |
while (uglyNum[index3] * 3 <= uglyNum[i - 1]) | |
index3++; | |
while (uglyNum[index5] * 5 <= uglyNum[i - 1]) | |
index5++; | |
uglyNum[i] = Math.min(uglyNum[index2] * 2, uglyNum[index3] * 3); | |
uglyNum[i] = Math.min(uglyNum[i], uglyNum[index5] * 5); | |
} | |
System.out.println("The 1500'th ugly number is " + uglyNum[1500] + "."); | |
} | |
} |
單字
** **
!! !!
片語 & 搭配詞
!! !!