55:丑数

向右看齐 2022-11-29 12:30 338阅读 0赞

丑数:因子只包含2,3,5的数,1为第一个丑数,2,3,5皆为丑数,请第1500个丑数。

  1. public class Offer55 {
  2. public static void main(String[] args) {
  3. System.out.println(new Offer55().getUglyNumber_solution(5));
  4. }
  5. public int getUglyNumber_solution(int index){
  6. if(index<=0)return 0;
  7. int[] temp = new int[index];
  8. temp[0]=1;
  9. int towIndex = 0;
  10. int threeIndex = 0;
  11. int fiveIndex = 0;
  12. for(int i=1;i<index;i++){
  13. int min = Math.min(Math.min(temp[towIndex]*2,temp[threeIndex]*3),temp[fiveIndex]*5);
  14. temp[i] = min;
  15. if(temp[i] == temp[towIndex]*2)towIndex++;
  16. if(temp[i] == temp[threeIndex]*3)threeIndex++;
  17. if(temp[i] == temp[fiveIndex]*5)fiveIndex++;
  18. }
  19. return temp[index-1];
  20. }
  21. }

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读

    相关

    一、题目大意 只包含因子2、3、5的数叫丑数,习惯上把1也看做丑数,求第1500个丑数。 二、分析 (1)常规做法,可对每个正整数依次遍历,直到找到第1500个丑数为止。

    相关

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

    相关

    \\题目描述 把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到

    相关

    把只包含因子2、3和5的数称作丑数,例如6,8都是丑数,但14不是,因为它包含因子7,习惯上我们把1当作第一个丑数,求按从小到大的顺序的第N个丑数。 输入描述:整数N

    相关

    求第1500个丑数。丑数是不能被2.3.5之外的其他素数整除的数,把丑数从小到大排起来,然后打印第1500个 先写一个典例,我写的, //这是我写的,运行了大概15

    相关

    时间限制:1秒 空间限制:32768K 热度指数:223966 本题知识点: 数组 算法知识视频讲解 题目描述 把只包含质因子2、3和5的数称作丑数(Ugly

    相关

    列表res按序存储丑数 res\[0\] = 1, 下一个丑数产生规则: 1.找出res所有数\2 中第一个 大于 res \[-1\]  的数:res\[n2\] 2

    相关

    丑数就是只包含质因数 2, 3, 5 的正整数。 1.判断丑数 2.找到第n个丑数 (代码很容易看懂) public class UglyNum {