~cpp //136 #include <iostream> #include <list> using namespace std; void main() { int cnt = 1500; list<unsigned int> numbers; numbers.push_back(1); unsigned int temp; while (cnt) { temp = numbers.front(); numbers.pop_front(); cnt--; numbers.push_back(temp * 2); numbers.push_back(temp * 3); numbers.push_back(temp * 5); numbers.sort(); numbers.unique(); } cout << "The 1500'th ugly number is " << temp << "." << endl; }
~cpp #include <iostream> using namespace std; void main() { int cnt; cin >> cnt; int num = 1, temp; cnt--; while (cnt) { num++; temp = num; while (temp % 2 == 0) temp = temp / 2; while (temp % 3 == 0) temp = temp / 3; while (temp % 5 == 0) temp = temp / 5; if (temp == 1) cnt--; } cout << num << endl; }