uglynumber的定义是只能被1,2,3,5整除的数

规定1是第一个uglynumber;以此类推,1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 ...

问题1,给定一个非零int整数,判断是否为ugly number

此数除以2,3,5;看是否能被整除;整除的判断是用hasDigit方法,利用小数来判断的

如果能被整除,递归下去,再除以2,3,5;直到完成判断

 /**
  * Ugly number1
  * @author Rust Fisher
  * Judge the number whether ugly
  */
 public class UglyNumber1 {
     public static boolean hasDigit(double d){
         return  d*10%10 != 0;
     }
     /**
      * @param num
      * @return boolean whether num is ugly
      */
     public static boolean isUgly(int num) {
         if (num <= 0) {
             return false;
         }
         if (num == 1) {
             return true;
         }
         if (!hasDigit(num/2.0)) {
             if (isUgly(num/2)) {
                 return true;
             }
         }
         if (!hasDigit(num/3.0)) {
             if (isUgly(num/3)) {
                 return true;
             }
         }
         if (!hasDigit(num/5.0)) {
             if (isUgly(num/5)) {
                 return true;
             }
         }

         return false;
     }
     /**
      * Find the nth ugly number
      * @param n
      * @return the nth ugly number
      */
     public static int nthUglyNumber(int n) {
         if (n <= 0) {
             return -1;
         }
         int count = 0;
         int i = 0;
         while (count <= n){
             if (isUgly(i)) {
                 count++;
             }
             if (count == n) {
                 break;
             }
             i++;

         }
         return i;
     }

     public static void main(String args[]){
         int count = 0;
         for (int i = 0; i < 100; i++) {
             if (isUgly(i)) {
                 count++;
                 System.out.print(i + "\t");
             }
             if (count == 10) {
                 count = 0;
                 System.out.println();
             }
         }
         System.out.println("\nThe n-th ugly numbers : ");
         count = 0;
         for (int i = 1; i < 21; i++) {
             System.out.print(nthUglyNumber(i) + "  ");
         }
         System.out.println("\n用这种方式输出第n个ugly number很没效率");
     }
 }

输出:

1	2	3	4	5	6	8	9	10	12
15	16	18	20	24	25	27	30	32	36
40	45	48	50	54	60	64	72	75	80
81	90	96
The n-th ugly numbers :
1  2  3  4  5  6  8  9  10  12  15  16  18  20  24  25  27  30  32  36
用这种方式输出第n个ugly number很没效率

问题2:求第n个ugly number

比如第1个ugly number是1,第二个是2,第三个是3 ...

已知1,2,3,5是ugly number,那么接下去的数能够乘以2,3,5得到;一个一个向后推算,直到第n个

设定3个游标,对应因数为2,3,5;利用到因数2一次,index2加一

 public class UglyNumber2{
     public static int getMin(int a,int b,int c){
         int min = a < b ? a : b;
         return min < c ? min : c;
     }
     public static int nthUglyNumber(int n) {
         if (n < 1) {
             return -1;
         }
         int index2 = 0, index3 = 0, index5 = 0;  //  three index
         int[] uglyNums = new int[n];
         uglyNums[0] = 1;
         int next = 1;
         while (next < n) {
             uglyNums[next] = getMin(uglyNums[index2]*2,uglyNums[index3]*3,uglyNums[index5]*5);
             if (uglyNums[next] == uglyNums[index2]*2) index2++;// find out which index should move
             if (uglyNums[next] == uglyNums[index3]*3) index3++;// index moving forward
             if (uglyNums[next] == uglyNums[index5]*5) index5++;
             next++;
         }
         return uglyNums[next - 1];
     }

     public static void main(String args[]){
         for (int i = 1; i < 21; i++) {
             System.out.print(nthUglyNumber(i) + " ");
         }
     }
 }

输出:

1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36

输出了前20个ugly number

UglyNumber - 找“丑数”的更多相关文章

  1. 剑指offer系列59---寻找丑数

    [题目]把只包含因子2.3和5的数称作丑数(Ugly Number). * 例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 解法一 ...

  2. 4 丑数 Ⅱ-找出第n个丑数

    原题网址:http://www.lintcode.com/zh-cn/problem/ugly-number-ii/ 设计一个算法,找出只含素因子2,3,5 的第 n 小的数. 符合条件的数如:1, ...

  3. 剑指offer-第5章优化时间和空间效率(丑数)

    题目:我们把只包含因子2,3,5的数叫做丑数.寻找第1500个丑数.通常把1当成第一个丑数. 思路1:第一步判断是否为丑数:丑数是只包含2,3,5的数,因此一定可以被2,3,5整除.通过求余数是否为零 ...

  4. 37.寻找丑数[Ugly numbers]

    [题目] 我们把只包含质因子2.3和5的数称作丑数(Ugly Number),例如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第1500个丑 ...

  5. 洛谷P2723 丑数 Humble Numbers

    P2723 丑数 Humble Numbers 52通过 138提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 对于一给定的素数 ...

  6. lintcode :Ugly Numbers 丑数

    题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...

  7. 剑指OFFER之丑数(九度OJ1214)

    题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 输入: 输 ...

  8. 丑数 LeeTCode

    题目链接:http://www.lintcode.com/zh-cn/problem/ugly-number-ii/ 题目描述:设计一个算法,找出只含素因子2,3,5 的第 n 大的数.符合条件的数如 ...

  9. Humble Numbers(丑数) 超详解!

    给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑 ...

随机推荐

  1. python中defaultdict的用法

    初识defaultdict 之前在使用字典的时候, 用的比较随意, 只是简单的使用dict. 然而这样在使用不存在的key的时候发生KeyError这样的一个报错, 这时候就该defaultdict登 ...

  2. html加javascript和canvas类似超级玛丽游戏

    html加javascript和canvas制作 代码来源于网上 复制可用 <!doctype html><html lang="en"> <head ...

  3. 【基础】C#异常处理的总结

    一.异常处理的理解? 异常处理是指程序在运行过程中,发生错误会导致程序退出,这种错误,就叫做异常. 因此处理这种错误,就称为异常处理. 二.异常处理如何操作? C# 异常处理时建立在四个关键词之上的: ...

  4. 初识 ActiveMQ

    其实算不上初识了,工作一年来一直都有接触 mq 相关的东西.但是,从来都是粘贴复制别人的配置代码,却从未认真系统的学习过它,现在线上用 mq 的项目出问题了,老板在后面拿枪指着呢,不得不好好研究下了. ...

  5. Unexpected end of input 和 Unexpected token var 和 Unexpected token ;

    在写jsp的时候使用的一段代码一直调试,出现Unexpected token ; 错误. 所以最后把代码各种精简,得到了如下的测试示例代码 <% String aaa="123&quo ...

  6. 《Python编程从入门到实践》第二章_变量和简单数据类型

    什么是变量呢? 举例: >>> message = "Hello,Python!" >>> print (message) Hello,Pyth ...

  7. windows环境下使用virtualenv对python进行多版本隔离

    最近在用python做一个文本的情感分析的项目,用到tensorflow,需要用python3的版本,之前因为<机器学习实战>那本书的缘故,用的是python2.7.所以目前的情况是要两个 ...

  8. 使用CSS3动画实现绚丽的照片墙效果

    临近毕业了,一大波毕业照又要来袭了!如何使用CSS3属性制作出自己的网页版照片墙呢? 闲话少说,先来看看效果图 效果要求: 1,照片要求有一定的白色边框. 2,照片都要有一定的倾斜角度. 3,鼠标移动 ...

  9. HTML5 中的拖放

    今天,给大家整理一个html5 拖放. 首先,我们先了解一下什么是拖放? 拖放(Drag 和 drop)是 HTML5 标准的组成部分. 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HT ...

  10. Linux: 安装和启用firefox浏览器的java

    之前在linux服务器上浏览一个网页时必须启用java才可以,但是自己只是一个服务器的普通用户,并没有root权限,所以只能把java装在自己的目录下面,因此不能用rpm包,而必须从Oracal官网下 ...