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 Specification
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 Specification
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
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.
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的数组来存放这些数,然后每次输入几,直接输出几。
注意:1.2.3跟其他数字后面跟的字母分别是st nd rd 11.12.13后面都是th
import java.util.Scanner;
public class ZOJ_1095 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int a2,a3,a5,a7,b2,b3,b5,b7,b=0;
int min;
int arr[]=new int [5842];
arr[b++]=1;
b7=0;b2=0;b3=0;b5=0;
while(b<5842){
a2=arr[b2]*2;
a3=arr[b3]*3;
a5=arr[b5]*5;
a7=arr[b7]*7;
min=Math.min(Math.min(a2, a3),Math.min(a7, a5));
arr[b++]=min;
if(min==a2)
b2++;
if(min==a3)
b3++;
if(min==a5)
b5++;
if(min==a7)
b7++;
}
while((b=s.nextInt())!=0){
System.out.print("The "+b);
switch(b%100){
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
System.out.print("th");
break;
default:
switch(b%10){
case 1:
System.out.print("st");
break;
case 2:
System.out.print("nd");
break;
case 3:
System.out.print("rd");
break;
default:
System.out.print("th");
}
}
System.out.println(" humble number is "+arr[b-1]+".");
}
}
}