-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.c
54 lines (44 loc) · 890 Bytes
/
7.c
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int reverse(int x)
{
int max = (int)pow(2, 31) - 1;
int min = -((int)pow(2, 31) + 1);
if (max <= x || min >= x)
{
return 0;
}
// printf("%d %d\n", max, min);
int absx = abs(x);
// printf("%d\n", absx);
double result = 0, m;
while (absx >= 10)
{
result *= 10;
m = absx % 10;
result += m;
absx /= 10;
}
result *= 10;
result += absx % 10;
// printf("\nresult:%.0f\n", result);
if (result > max)
{
result = 0;
}
if (x >= 0)
{
return (int)result;
}
else
{
return (int)result * -1;
}
}
int main(int argc, char const *argv[])
{
int result = reverse(12346); //1534236469
printf("%d", result);
return 0;
}