Uglynumber is defined as a number that can be divisible only by 1,2,3,5.
Rule 1 is the first uglynumber; and so on, 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 ...
Question 1, given a non-0 int integer, determines whether the ugly number
Divide this number by 2,3,5; see if it can be divisible; the judgment of dividing is by Hasdigit method, using decimal to judge
If it can be divisible, recursively, and divided by 2,3,5, until the judgment is complete.
1 /**2 * Ugly number13 * @authorRust Fisher4 * Judge The number whether ugly5 */6 Public classUglyNumber1 {7 Public Static BooleanHasdigit (Doubled) {8 returnD*10%10! = 0;9 }Ten /** One * @paramNum A * @returnboolean whether num is ugly - */ - Public Static Booleanisugly (intnum) { the if(Num <= 0) { - return false; - } - if(num = = 1) { + return true; - } + if(!hasdigit (num/2.0)) { A if(Isugly (NUM/2)) { at return true; - } - } - if(!hasdigit (num/3.0)) { - if(Isugly (NUM/3)) { - return true; in } - } to if(!hasdigit (num/5.0)) { + if(Isugly (NUM/5)) { - return true; the } * } $ Panax Notoginseng return false; - } the /** + * Find the nth ugly number A * @paramN the * @returnThe nth Ugly number + */ - Public Static intNthuglynumber (intN) { $ if(n <= 0) { $ return-1; - } - intCount = 0; the inti = 0; - while(Count <=N) {Wuyi if(isugly (i)) { thecount++; - } Wu if(Count = =N) { - Break; About } $i++; - - } - returni; A } + the Public Static voidMain (String args[]) { - intCount = 0; $ for(inti = 0; I < 100; i++) { the if(isugly (i)) { thecount++; theSystem.out.print (i + "\ T")); the } - if(Count = = 10) { inCount = 0; the System.out.println (); the } About } theSystem.out.println ("\nthe n-th ugly Numbers:"); theCount = 0; the for(inti = 1; I < 21; i++) { +System.out.print (Nthuglynumber (i) + ""); - } theSystem.out.println ("\ nthe output of nth ugly number in this way is inefficient");Bayi } the}
Output:
1234568910121516182024252730323640454850546064727580819096The n-th Ugly numbers:1 2 3 4 5 6 8 9 ( 36)-Ten the output of nth ugly number in this way is inefficient.
Issue 2: Number of nth ugly number
For example, the 1th ugly number is 1, the second one is 2, the third is 3 ...
It is known that 1,2,3,5 is ugly number, then the numbers can be multiplied by 2,3,5, one by one backwards, until the nth
Set 3 cursors, the corresponding factor is 2,3,5, using the factor 21 times, Index2 plus a
1 Public classuglynumber2{2 Public Static intGetmin (intAintBintc) {3 intMin = a < b?a:b;4 returnMin < c?Min:c; 5 }6 Public Static intNthuglynumber (intN) {7 if(N < 1) {8 return-1;9 }Ten intIndex2 = 0, index3 = 0, index5 = 0;//Three index One int[] Uglynums =New int[n]; AUglynums[0] = 1; - intNext = 1; - while(Next <N) { theUglynums[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++; - } + returnUglynums[next-1]; A } at - Public Static voidMain (String args[]) { - for(inti = 1; I < 21; i++) { -System.out.print (Nthuglynumber (i) + ""); - } - } in}
Output:
Outputs the first 20 ugly number
Uglynumber-Find "ugly number"