-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverse.cpp
More file actions
52 lines (47 loc) · 1.37 KB
/
reverse.cpp
File metadata and controls
52 lines (47 loc) · 1.37 KB
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
#include <iostream>
using namespace std;
int main() {
int number;
cin >> number;
int i = 10;
int reversed = 0;
int maxi = 1;
while (number % maxi != number) {
maxi = maxi * 10;
}
// cout << maxi << endl;
bool breakNextTime = false;
while (true) {
if (breakNextTime) {
// cout << "breaking" << endl;
break;
}
int result = number % i;
if (result == number) {
// we have breached the max length
breakNextTime = true;
}
if (result == 0) {
// units digit 0?
// cout << "digit 0?" << endl;
i = i * 10;
continue;
}
// cout << "i = " << i << " result = " << result << " i/result: " << i / result << endl;
// cout << "we got result as: " << result << " and modulas as " << result / 10 << endl;
if (i / result > 10) {
// we got a 0
i = i * 10;
continue;
}
while (result >= 10) {
result = result / 10;
// cout << " |- result getting reduced: " << result << endl;
}
// cout << endl;
// cout << "final result: " << result << " and multiply by " << i/10 << endl << endl;
reversed = reversed + (result * (maxi / i));
i = i * 10;
}
cout << reversed << endl;
}