-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathugly2.cpp
More file actions
39 lines (35 loc) · 739 Bytes
/
ugly2.cpp
File metadata and controls
39 lines (35 loc) · 739 Bytes
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
#include "lib/misc.h"
class Solution {
public:
bool isUgly(int n) {
if (n == 1) return true;
vector<int> ugly = { 2, 3, 5 };
for (auto i : ugly) {
while (n % i == 0) {
n /= i;
}
}
return n == 1;
}
int nthUglyNumber(int n) {
int count = 0;
int nth = 0;
for (int i = 1; ; i++) {
if (isUgly(i)) {
printf("true %d, count %d \n", i, count);
if (++count == n) {
nth = i;
break;
}
}
}
return nth;
}
};
int main()
{
Solution s;
auto ans = s.nthUglyNumber(322);
printf("ans - %d \n", ans);
return 0;
}