1. Normal is the number one traversal then see which is the number of ugly?
// face questions 49: Number ugly // Title: We only comprise factors 2, 3 and 5 is referred to as the number of ugly number (Ugly Number). Seeking ascending // of 1500 the number of ugly big order. E.g. 6,8 are several ugly, but not 14, because it contains factor 7. // practice, we put 1 as the first ugly number. #include <the iostream> // Code Algorithm 1 =================== ==================== = // no additional memory, direct calculation BOOL IsUgly ( int number) // judgment is not ugly number of { the while (number% 2 == 0 ) number / = 2 ; the while (number% . 3 == 0 ) number / = 3 ; The while (Number% . 5 == 0 ) Number / = . 5 ; return (== Number . 1 )? To true : to false ; } int GetUglyNumber_Solution1 ( int index) { IF (index <= 0 ) return 0 ; int Number = 0 ; int uglyFound = 0 ; the while (uglyFound <index) // start to finish counted { ++number; if (IsUgly(number)) ++uglyFound; } return number; }
2. cattle off network was amazing
int GetUglyNumber_Solution ( int index) { IF (index < . 7 ) return index; Vector < int > RES (index); RES [ 0 ] = . 1 ; // called customary ugly first number. 1 int T2 = 0 , T3 = 0 , T5 = 0 , I; for (I = . 1 ; I <index; ++ I) { RES [I] = min (RES [T2] * 2 , min (RES [T3] * . 3 , RES [T5 ] * 5)); if (res[i] == res[t2] * 2)t2++; if (res[i] == res[t3] * 3)t3++; if (res[i] == res[t5] * 5)t5++; } return res[index - 1]; }