d The first ugly number is 1,1*2,1*3,1*5 the smallest number is 2, then the second ugly number is 2, then 1*3,2*2,1*5 the smallest is 3, the third ugly number is 3, and then 2*3,2*2,1*5 the smallest number is 4, so the fourth ugly number is 4, when each found an ugly number, In order to record the current number of ugly subscript and the previous two unselected numbers of subscript, according to these three numbers to update the ugly number in turn
1#include <iostream>2#include <algorithm>3 using namespacestd;4 intresult[1501];5 6 voidInit ()7 {8 intP1 =1, p2 =1, p3 =1;9result[1] =1;Ten for(inti =2; I <= the; ++i) One { AResult[i] = min (result[p1]*2, Min (result[p2]*3, result[p3]*5)); - - if(Result[i] = = result[p1]*2) ++P1; the if(Result[i] = = result[p2]*3) ++P2; - if(Result[i] = = result[p3]*5) ++P3; - } - } + - intMain () + { A intN; at init (); - while(Cin >> N &&N) -cout << Result[n] <<Endl; - return 0; -}
1#include <iostream>2#include <algorithm>3 using namespacestd;4 intresult[1501];5 6 voidInit ()7 {8 intP1 =1, p2 =1, p3 =1;9result[1] =1;Ten for(inti =2; I <= the; ++i) One { AResult[i] = min (result[p1]*2, Min (result[p2]*3, result[p3]*5)); - - if(Result[i] = = result[p1]*2) ++P1; the if(Result[i] = = result[p2]*3) ++P2; - if(Result[i] = = result[p3]*5) ++P3; - } - } + - intMain () + { A intN; at init (); - while(Cin >> N &&N) -cout << Result[n] <<Endl; - return 0; -}
POJ 1338 Ugly Numbers