ZOJ--1095:Humble Numbers
2017-06-19 13:16
489 查看
Humble Numbers
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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.
n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for
n.
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.
2
3
4
11
12
13
21
22
23
100
1000
5842
0
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
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 integern 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 isnumber.". 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
12
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的数组来存放这些数,然后每次输入几,直接输出几。
注意: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]+"."); } } }
相关文章推荐
- ZOJ 1095 Humble Numbers (构造)
- ZOJ 1095 Humble Numbers
- zoj 1095 || hdu 1058 Humble Numbers( DP || 乱搞)
- ZOJ 1095 Humble Numbers
- zoj 1095 Humble Numbers
- zoj1095 Humble Numbers(DP)
- zoj 1095 Humble Numbers
- zoj 1095 Humble Numbers(丑数+dp)
- zoj 1095 - Humble Numbers
- ZOJ 1095 Humble Numbers
- zoj1095 Humble Numbers
- 1095 Humble Numbers
- Raising Modulo Numbers(ZOJ 2150)
- 3.1.3 Humble Numbers
- HDU 1058 Humble Numbers
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
- HDU1058-Humble Numbers
- UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
- HDU:1058 Humble Numbers(技巧打表)
- ZOJ 1383: Binary Numbers