-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
33 lines (31 loc) · 918 Bytes
/
program.cpp
File metadata and controls
33 lines (31 loc) · 918 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
#include "../include/pre.h"
int reverse(int x)
{
int flag = x < 0 ? 1 : 0;
auto willOverflow = [=](int rst, int next){
const int MAX = std::numeric_limits<int>::max();
rst = std::abs(rst);
if (rst > MAX / 10) return true;
//It is of no use to check this condition since the INT32_MAX is 2147483647
// 2 < 7 or 2 < 8 when negative
//However, to make sure for other platform relevantd int, I think it is integral.
// e.g. int64 : 9,223,372,036,854,775,807
if (rst == MAX / 10 && next > x % 10 + flag) return true;
return false;
};
int rst = 0;
int i = 0;
do
{
if (++i > std::numeric_limits<int>::digits10 - 1 && willOverflow(rst, x % 10)) return 0;
rst = rst * 10 + x % 10;
x /= 10;
}
while (x);
return rst;
}
int main()
{
cout << reverse(2147483647);
return 0;
}