`
xiaoy2002
  • 浏览: 5482 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Ugly Numbers

阅读更多
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.

Write a program to find and print the 1500'th ugly number.

Input and Output:
There is no input to this program. 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>.

  

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence


1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...



shows the first 11 ugly numbers. By convention, 1 is included.


Write a program to find and print the 1500'th ugly number.


Input and Output
There is no input to this program. 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>.





public class UglyNumber {
   public static void main(String[] args){
       int m2 = 0;
       int m3 = 0;
       int m5 = 0;
       int[] a = new int[1500];
 
       int temp = 1;
       a[0] = 1;
 
       for(int i = 1; i < 1500;i++){
         temp = 2*a[m2] > 3*a[m3] ? 3*a[m3] : 2*a[m2];
         temp = temp > 5*a[m5] ? 5*a[m5] : temp;

         if(temp == 2*a[m2])
            m2++;
         if(temp == 3*a[m3])
            m3++;
         if(temp == 5*a[m5])
            m5++;
  
         a[i] = temp;
  }
  System.out.println("The 1500'th ugly number is: " + a[1499]);
}
}

算法思想:满足条件的数一定是2或3或5的倍数(包括1)。设置三个变量m2,m3,m5,表示下一个满足条件的数是2*a[m2],3*a[m3]和5*a[m5]这三个数中的最小者。满足条件的 m 值加1.下次从新的 m 值乘以相应的倍数。如要求a[8]时,m2 = 4,m3 = 3, m5 = 1,由于2 * a[4] = 10 及5 * a[1] = 10 小于 3 * a[3] = 12,这时可得a[8] = 10,同时
m2 = m2 + 1 = 5, m5 = m5 + 1 = 2.

分享到:
评论

相关推荐

    pku acm1338 Ugly Numbers 代码

    pku acm1338 Ugly Numbers 代码 动态规划思想,采用链表实现,解题报告请访问:http://blog.csdn.net/china8848

    pku acm 1338 Ugly Numbers代码

    pku acm 1338 Ugly Numbers代码 动态规划思想,数组实现 解题报告请访问:http://blog.csdn.net/china8848

    Codeforces Global Round 7 A. Bad Ugly Numbers

    Bad Ugly Numbers 题目链接-A. Bad Ugly Numbers 题目大意 输出一个位数为n的数s,且该数每一位数字都不能被s整除 解题思路 贪心 如果n为1,那么无论s是哪个数字都必定能整除自身 如果n不为n,那么577…77和233…...

    Codeforces Global Round 7 A. Bad Ugly Numbers(思维)

    传送门 题意: 给一个n,输出一个长度为n的数,这个数不能整数每一位 思路: 2333333333即可 是奇数,所以不能整除2,所有数的和等于3*(n-1)+2,不是3的倍数,所以不能整除3 代码: #include #include ...

    【数学】C009_丑数(整除 | 递归)

    Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. Input: 6 Output: true Explanation: 6 = 2 × 3 Note: 1 is typically treated as an ugly number. Input is within the 32-bit ...

    ugly-numbers:丑陋的数字问题出现在Google问题列表中。 也在代码评估中

    丑数 丑陋的数字问题出现在Google问题列表中。 也在代码评估中 丑陋的数字 挑战说明: 积分:这项挑战之前曾在Google竞赛中出现过。 很久以前,在一个奇怪的情况下,如果一个数字可以被任何一位数的质数(2、3、5...

    cpp-算法精粹

    Bitwise AND of Numbers Range Power of Three Rectangle Area 数论 Happy Number Ugly Number Ugly Number II Super Ugly Number Fraction to Recurring Decimal Factorial Trailing Zeroes Nim Game 模拟 Reverse ...

    leetcode:LeetCode解决方案,用python和cpp编写(LeetCode解题报告,记录自己的leetcode生长之路)

    = 2 ^ 31,O(T)= 32 问题解时间空间困难注意年级002.add-two-numbers ,上) O(1) 中/ S-- 007.反向整数 ,O(长) O(1) 简易/ S-- 008.字符串到整数 ,上) O(1) 中/ S-- 溢出检查太棒了009.回文数上) O...

    LeetCode最全代码

    201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...

    VB编程资源大全(英文源码 其它)

    Great of coming up with random passwords for your applications.&lt;END&gt;&lt;br&gt;42,norepeat.zip A simple program that generates non repeating numbers at random in certain ranges.&lt;END&gt;&lt;br&gt;44,vbcomment.zip...

    iOS-2048-master

    ...except looping it is a bit ugly, so I made a `forEach` helper function. * The `M2Cell` class is the "slot"s. They are not the tiles themselves. The benefit of having this class is that the cells ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Table of Contents Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes ...

Global site tag (gtag.js) - Google Analytics