HDOJ1058Humble Numbers(丑数)
来源:互联网 发布:mac笔记本如何收藏网页 编辑:程序博客网 时间:2024/06/05 15:03
Humble Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27739 Accepted Submission(s): 12234
Problem Description
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
Write a program to find and print the nth element in this sequence
Input
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
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
1234111213212223100100058420
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.
这里对丑数的定义和百科上面的有所区别,百科上面讲的是2,3,5.而这里讲的多了一个7.
百科对丑数的定义是只包含因子2,3,5的正整数被称作丑数,比如4,10,12都是丑数,而7,23,111则不是丑数,另外1也不是丑数。(这里多了7)
判断是只包含因子2,3,5,7可以使用:首先除2,直到不能整除为止,然后除5到不能整除为止,然后除3直到不能整除为止,然后除7到不能整除为止。。。最终判断剩余的数字是否为1,如果是1则为丑数,否则不是丑数。
题目是要输出指定位置的丑数。
但是这种方法我测试了下太耗时了。
代码如下:
arr = new int[5843];arr[1] = 1;int j = 2;int i = 2;while (j < 5843) {int num = i;// 判断是否有质因子2// 如果N中含有质因子2,把所有的因子2都去掉while (num % 2 == 0) {num /= 2;}while (num % 3 == 0) {num /= 3;}while (num % 5 == 0) {num /= 5;}while (num % 7 == 0) {num /= 7;}if (num == 1) {// 如果最后N除的只剩1,则N就是丑数arr[j] = i;//System.out.println(arr[j]);j++;}i++;}我们可以使用另一种方法来算出丑数,就是从以1为"始祖",剩下的所有其他的数,其实都是在此基础上乘以2,3,5,7演化出来的。
AC代码如下:
import java.util.Scanner;public class Main{private static Scanner scanner;private static int arr[];private static int i = 1, j = 1, k = 1, l = 1;public static void main(String[] args) {scanner = new Scanner(System.in);dabiao();while (scanner.hasNext()) {int n = scanner.nextInt();if (n == 0) {break;}// 1,2,3分别是st,nd,rd,其他是thif (n % 10 == 1 && n % 100 != 11) {System.out.println("The " + n + "st humble number is " + arr[n] + ".");} else if (n % 10 == 2 && n % 100 != 12) {System.out.println("The " + n + "nd humble number is " + arr[n] + ".");} else if (n % 10 == 3 && n % 100 != 13) {System.out.println("The " + n + "rd humble number is " + arr[n] + ".");} else {System.out.println("The " + n + "th humble number is " + arr[n] + ".");}}}private static void dabiao() {arr = new int[5843];arr[1] = 1;for (int t = 2; t < arr.length; t++) {//核心代码!!arr[t] = min(2 * arr[i], 3 * arr[j], 5 * arr[k], 7 * arr[l]);}}private static int min(int a, int b, int c, int d) {int m = a;if (m > b)m = b;if (m > c)m = c;if (m > d)m = d;if (a == m)i++;if (b == m)j++;if (c == m)k++;if (d == m)l++;return m;}}
阅读全文
0 0
- HDOJ1058Humble Numbers(丑数)
- Ugly Numbers(丑数)
- Humble Numbers(丑数)
- POJ 1338 Ugly Numbers(丑数)
- poj1338 Ugly Numbers(丑数模拟)
- UVA136 - Ugly Numbers(丑数)
- [luogu2723]丑数 Humble Numbers(乱搞)
- 丑数 Ugly Numbers
- Humble Numbers 丑数
- Ugly Numbers --丑数
- NYOJ1097 Ugly Numbers 【丑数】
- 158ugly numbers丑数
- USACO : Humble Numbers (丑数)
- Ugly Numbers(POJ1338)(丑数,技巧性强)
- poj 1338 Ugly Numbers(丑数模拟)
- HDOJ题目1058Humble Numbers(数学,丑数)
- HDU 1058 Humble Numbers(丑数,优先队列,STL)
- hdu1058 Humble Numbers(丑数) —— dp
- fragment配合viewpager实现懒加载
- java加载资源文件的两种方法
- 作业: 一.使用泛型版的ArrayList集合,对其添加100个不同的整数: 1.使用add()方法将100个整数添加到ArrayList集合中; 2.调用集合的iterator()方法获得Itera
- 2017_11_29 ajax请求的get,post方式
- 运动控制卡与PLC
- HDOJ1058Humble Numbers(丑数)
- (一)java基础篇笔记库(11)
- #算法之路之征服上海交大的oj-高精度加法
- BZOJ3675 [Apio2014]序列分割 【斜率优化dp】
- 35-关于日志Log输出插件Log4Net的总结
- POJ 1251 Jungle Roads(最小生成树简单题)
- LDA相关改进
- 哪款播放器好
- 算法入门经典第二版 3-8 Repeating Decimals