网站改版阿里云怎么做网站301定向,温州网站运营,wordpress 怎么重新安装,云卡会员管理系统编写一个程序#xff0c;找出第 n 个丑数。
丑数就是只包含质因数 2, 3, 5 的正整数。
示例:
输入: n 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 说明:
1 是丑数。 n 不超过1690。
思路#xff1a;从1开始#xff0c;需要对每个值都乘以2…编写一个程序找出第 n 个丑数。
丑数就是只包含质因数 2, 3, 5 的正整数。
示例:
输入: n 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 说明:
1 是丑数。 n 不超过1690。
思路从1开始需要对每个值都乘以235然后判断大小把他们依次放入数组
设置三个指针位各自独立地向后遍历实现这个目标
提交的代码
class Solution { public int nthUglyNumber(int n) { int x,y,z,i,j1; int[] nums new int[1690]; nums[0] 1; x 0; y 0; z 0; for(i1;in;i) { j java.lang.Math.min(nums[x]*2, java.lang.Math.min(nums[y]*3,nums[z]*5)); if(nums[x]*2j) { nums[i]j; x; } if(nums[y]*3j) //不可用else if否则如果x位于第三个位置3处y位于第二个位置2处会出现重复的数字2*33*2 { nums[i]j; y; } if(nums[z]*5j) { nums[i]j; z; } } return nums[n-1]; } }
完整的代码 import java.util.Scanner;
public class Soluiton264 { public static int nthUglyNumber(int n) { int x,y,z,i,j1; int[] nums new int[1690]; nums[0] 1; x 0; y 0; z 0; for(i1;in;i) { j java.lang.Math.min(nums[x]*2, java.lang.Math.min(nums[y]*3,nums[z]*5)); if(nums[x]*2j) { nums[i]j; x; } if(nums[y]*3j) { nums[i]j; y; } if(nums[z]*5j) { nums[i]j; z; } } return nums[n-1]; } public static void main(String[] args) { Scanner scanner new Scanner(System.in); int x scanner.nextInt(); System.out.println(nthUglyNumber(x)); } }