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