Title Description
It contains only the number of prime factors 2, 3 and 5 is referred to as the number of ugly (Ugly Number). E.g. 6,8 are several ugly, but not 14, because it contains seven prime factors. Traditionally we have 1 as the first ugly number. Seeking ascending through N ugly large number sequence.
1 // Use traverse determination method 2 class Solution01 { . 3 public : . 4 int GetUglyNumber_Solution ( int index) { . 5 IF (index < 1 ) return 0 ; . 6 int CNT = 0 ; . 7 for ( int I = 1 ; CNT <index ; ++ I) . 8 { . 9 IF (isChouShu (I)) 10 CNT ++ ; . 11 IF (CNT == index) 12 return i; 13 } 14 } 15 bool isChouShu ( int num) 16 { 17 while (in% 2 == 0 ) 18 a / = 2 ; 19 while (in% 3 == 0 ) 20 a / = 3 ; 21 while (in% 5 == 0 ) 22 a / = 5 ; 23 return== NUM . 1 ; 24 } 25 }; 26 is 27 28 // use the stored data Method 29 class Solution02 { 30 public : 31 is int GetUglyNumber_Solution ( int index) { 32 IF (index < . 1 ) return 0 ; 33 is Vector < int > V (index, 0 ); 34 is V [ 0 ] = . 1 ; 35 int P2, P3, P5, K = . 1 ; 36 p2 = p3 = p5 = 0 ; 37 while (k < index) 38 { 39 v [k ++] = min ( min (v [p2] * 2 , v [p3] * 3 ), v [p5] * 5 ); 40 The while (v [p2] * 2 <= v [k - 1 ]) p2 ++ ; 41 while (v [p3] * 3 <= v [k - 1 ]) ++ p3 ; 42 The while (v [p5] * 5 <= v [k - 1 ]) p5 ++ ; 43 } 44 return v[index - 1]; 45 } 46 };