-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
50 lines (44 loc) · 1.03 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Author : Qi Zhang
// Date : 2018-12-11
#include <bits/stdc++.h>
using namespace std;
long long nthUglyNumber(int n) {
queue<long long> two, three, five;
two.push(2);
three.push(3);
five.push(5);
long long cur = 1;
while(n-- > 1){
long long a = two.front(), b = three.front(), c = five.front();
if(a < b && a < c){
cur = a;
two.pop();
two.push(cur * 2);
three.push(cur * 3);
five.push(cur * 5);
}
if(b < a && b < c){
cur = b;
three.pop();
three.push(cur * 3);
five.push(cur * 5);
}
if(c < a && c < b){
cur = c;
five.pop();
five.push(cur * 5);
}
//cout << a << ", " << b << ", " << c << endl;
//if(n < 100) cout << cur << endl;
}
return cur;
}
int main()
{
string line;
while (getline(cin, line)) {
int n = stoi(line);
cout << nthUglyNumber(n) << endl;
}
return 0;
}