zoukankan      html  css  js  c++  java
  • 剑指Offer(Java版)第三十八题:把只包含质因子2、3和5的数称作丑数(Ugly Number)。 例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。 求按从小到大的顺序的第N个丑数。

    /*
    把只包含质因子2、3和5的数称作丑数(Ugly Number)。
    例如6、8都是丑数,但14不是,因为它包含质因子7。
    习惯上我们把1当做是第一个丑数。
    求按从小到大的顺序的第N个丑数。
    */
    public class Class38 {

    public int GetUglyNumber_Solution(int index){
    if(index <= 0){
    return 0;
    }
    int count = 0;
    int uglyNumber = 0;
    int start = 1;
    while(count < index){
    if(findUglyNumber(start) == true){
    uglyNumber = start;
    count++;
    start++;
    }else{
    start++;
    }
    }
    return uglyNumber;
    }

    public boolean findUglyNumber(int index){
    if(index <= 0){
    return false;
    }
    while(true){
    if(index == 1 || index == 2 || index == 3 || index == 5){
    return true;
    }else if(index % 2 == 0){
    index /= 2;
    }else if(index % 3 == 0){
    index /= 3;
    }else if(index % 5 == 0){
    index /= 5;
    }else{
    return false;
    }
    }
    }
    public void test(){
    int index = 20;
    System.out.println(GetUglyNumber_Solution(index));
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Class38 c = new Class38();
    c.test();

    }

    }

  • 相关阅读:
    SAR图像处理 MSTAR数据库利用问题
    python 获取系统环境变量 os.environ and os.putenv
    python 模块中的 __init__.py __main__.py
    pylint python2.7 安装记录
    Python--字典
    哈希表
    AC自动机模板
    平衡树(Splay)模板
    矩阵快速幂 模板
    非递归线段树
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12517775.html
Copyright © 2011-2022 走看看