-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUgly Numbers.cpp
More file actions
58 lines (50 loc) · 892 Bytes
/
Ugly Numbers.cpp
File metadata and controls
58 lines (50 loc) · 892 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
unordered_map<int, int> um;
int findUgly(int);
int isUgly(int);
int maxDivide(int, int);
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
cout << findUgly(n) << endl;
}
return 0;
}
int findUgly(int n){
int i = 1;
int count = 1;
while (n > count){
i++;
if (isUgly(i))
count++;
}
return i;
}
int maxDivide(int a, int b)
{
while (a%b == 0){
a = a/b;
if(um.find(no) != um.end())
return um[no];
}
return a;
}
int isUgly(int no){
if(um.find(no) != um.end())
return um[no];
int temp = no;
temp = maxDivide(temp, 2);
temp = maxDivide(temp, 3);
temp = maxDivide(temp, 5);
if(temp == 1){
um[no] = 1;
return 1;
}
else{
um[no] = 0;
return 0;
}
}