File tree Expand file tree Collapse file tree 2 files changed +76
-1
lines changed Expand file tree Collapse file tree 2 files changed +76
-1
lines changed Original file line number Diff line number Diff line change @@ -58,4 +58,8 @@ docs/_build/
58
58
# PyBuilder
59
59
target /
60
60
61
- test.py
61
+ test.py
62
+
63
+ # c out file
64
+ * .out
65
+ * .exe
Original file line number Diff line number Diff line change
1
+ /**
2
+ *本题目使用C编写,因为Python的int类型是无限大的,要判断一个数字是否溢出非常简单,祝需要和2^32-1比较就好了
3
+ */
4
+
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <limits.h>
8
+
9
+
10
+ int intToString (int i ,char * arr ){
11
+ int t_len = sprintf (arr ,"%d" ,i );
12
+ arr [t_len ] = '\0' ;
13
+ return t_len ;
14
+ }
15
+ int willOverflow (char * result ,int is_negative ){
16
+ char min_str [40 ];
17
+ char max_str [40 ];
18
+ int max_len = intToString (INT_MAX ,max_str );
19
+ int min_len = intToString (INT_MIN ,min_str );
20
+ int result_len = strlen (result );
21
+ if ((is_negative && result_len < min_len ) || (!is_negative && result_len < max_len )){
22
+ return 0 ;
23
+ }
24
+ return is_negative ?(strcmp (min_str ,result )< 0 ):(strcmp (max_str ,result )< 0 );
25
+ }
26
+ int reverse (int x ) {
27
+ char * s_char ,* tar_c ;
28
+ int result = 0 ;
29
+ char intStr [40 ];
30
+ int is_negative = 0 ;
31
+ tar_c = intStr ;
32
+ if (x < 0 ){
33
+ * tar_c = '-' ;
34
+ tar_c ++ ;
35
+ x = - x ;
36
+ is_negative = 1 ;
37
+ }
38
+
39
+ while (x > 0 ){
40
+ * tar_c = x %10 + '0' ;
41
+ x /= 10 ;
42
+ tar_c ++ ;
43
+ }
44
+ * tar_c = '\0' ;
45
+ if (willOverflow (intStr ,is_negative )){
46
+ return 0 ;
47
+ }
48
+ for (tar_c = intStr ;(* tar_c )!= '\0' ;tar_c ++ ){
49
+ if ((* tar_c )!= '-' ){
50
+
51
+ result *= 10 ;
52
+ result += (* tar_c )- '0' ;
53
+ }
54
+ }
55
+ if (intStr [0 ]== '-' ){
56
+ result = - result ;
57
+ }
58
+
59
+ return result ;
60
+ }
61
+
62
+
63
+
64
+ int main (){
65
+ printf ("%d\n" ,reverse (1231312 ));
66
+ printf ("%d\n" ,reverse (123 ));
67
+ printf ("%d\n" ,reverse (-1231312 ));
68
+ printf ("%d\n" ,reverse (1000000003 ));
69
+ printf ("%d\n" ,reverse (-1000000003 ));
70
+ return 0 ;
71
+ }
You can’t perform that action at this time.
0 commit comments