Skip to content

Commit 9046709

Browse files
committed
add code for ugly numbers
1 parent cb4c016 commit 9046709

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

Diff for: a.out

-3.95 KB
Binary file not shown.

Diff for: ugly-number.cpp

+54-33
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,62 @@ int main(){
66
int n;
77
int arr[100005], res[100005];;
88
cin>>n;
9-
int sz = 100005;
10-
for(int i=0;i<sz;i++){
11-
arr[i] = 0;
12-
}
13-
arr[0] = 0;
14-
arr[1] = 1;
15-
arr[2] = 1;
16-
arr[3] = 1;
17-
arr[4] = 1;
18-
arr[5] = 1;
19-
res[1] = 1;
20-
res[2] = 2;
21-
res[3] = 3;
22-
res[4] = 4;
23-
res[5] = 5;
24-
int j = 6;
25-
for(int i=6; i<sz; i++){
26-
if(i%5 == 0){
27-
if(arr[i/5] == 1){
28-
arr[i] = 1;
29-
res[j++] = i;
30-
}
31-
}else if(i%3 == 0){
32-
if(arr[i/3] == 1){
33-
arr[i] = 1;
34-
res[j++] = i;
35-
}
36-
}else if(i%2 == 0){
37-
if(arr[i/2] == 1){
38-
arr[i] = 1;
39-
res[j++] = i;
40-
}
9+
int i2=0,i3=0,i5=0;
10+
int two=2,three=3,five=5;
11+
int next = 1;
12+
arr[0]=1;
13+
for(int i=1;i<n;i++){
14+
next = min(two,min(three,five));
15+
arr[i] = next;
16+
if(next == two){
17+
i2++;
18+
two = arr[i2]*2;
19+
}
20+
if(next == three){
21+
i3++;
22+
three = arr[i3]*3;
23+
}
24+
if(next == five){
25+
i5++;
26+
five = arr[i5]*5;
4127
}
4228
}
43-
cout << res[n] << endl;
29+
cout << next << endl;
30+
// int sz = 100005;
31+
// for(int i=0;i<sz;i++){
32+
// arr[i] = 0;
33+
// }
34+
// arr[0] = 0;
35+
// arr[1] = 1;
36+
// arr[2] = 1;
37+
// arr[3] = 1;
38+
// arr[4] = 1;
39+
// arr[5] = 1;
40+
// res[1] = 1;
41+
// res[2] = 2;
42+
// res[3] = 3;
43+
// res[4] = 4;
44+
// res[5] = 5;
45+
// int j = 6;
46+
// for(int i=6; i<sz; i++){
47+
// if(i%5 == 0){
48+
// if(arr[i/5] == 1){
49+
// arr[i] = 1;
50+
// res[j++] = i;
51+
// }
52+
// }else if(i%3 == 0){
53+
// if(arr[i/3] == 1){
54+
// arr[i] = 1;
55+
// res[j++] = i;
56+
// }
57+
// }else if(i%2 == 0){
58+
// if(arr[i/2] == 1){
59+
// arr[i] = 1;
60+
// res[j++] = i;
61+
// }
62+
// }
63+
// }
64+
// cout << res[n] << endl;
4465

4566
return 0;
4667
}

0 commit comments

Comments
 (0)