-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode7.cpp
51 lines (48 loc) · 823 Bytes
/
leetcode7.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
51
#include <iostream>
#include <cmath>
using namespace std;
int reverse(int x) {
int size = 0;
int result = 0;
int flag = 0;
int num = x;
// calculate the size
if(x < 0)
{
x = -x;
flag = 1;
}
if(x < 10)
{
if(flag == 1)
return -x;
else
return x;
}
while(num != 0)
{
num = num / 10;
size++;
}
//calculate reserve
int len = size;
while(size > 0)
{
int tmp = pow(10,size-1)+0.5;
result = result + tmp * (x % 10);
x = x / 10;
size--;
}
if(flag == 1)
return -result;
else
return result;
}
int main(int argc, char const *argv[])
{
int num;
cin >> num;
num = reverse(num);
cout << num << endl;
return 0;
}