summaryrefslogtreecommitdiff
path: root/0007/main.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-10-13 18:41:46 +0800
committerMistivia <i@mistivia.com>2025-10-13 18:41:46 +0800
commitb298f6a131b6412cdecba6317dc00e6ef5f46aca (patch)
tree968a36df1cd466f559720fe4c102064ea2158c6c /0007/main.c
parent16f799a0e089b1b0301b2ee6fc3b3d2c210d1d4b (diff)
solve 7
Diffstat (limited to '0007/main.c')
-rw-r--r--0007/main.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/0007/main.c b/0007/main.c
new file mode 100644
index 0000000..a110e30
--- /dev/null
+++ b/0007/main.c
@@ -0,0 +1,52 @@
+#include <stdint.h>
+#include <limits.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+int reverse(int x){
+ if (x == INT_MIN) {
+ return 0;
+ }
+ char buf[10];
+ memset(buf, 0, 10);
+ int i = 0;
+ bool pos = x > 0 ? true : false;
+ uint32_t ux = x > 0 ? x : -x;
+ while (ux > 0) {
+ buf[i] = ux % 10;
+ ux = ux / 10;
+ i++;
+ }
+ uint32_t r = 0;
+ int len = i;
+ for (int i = 0; i < len && i < 9; i++) {
+ r = r * 10 + buf[i];
+ }
+ if (len <= 9) {
+ return pos ? r : -r;
+ }
+ if (r < 214748364) {
+ r = r * 10 + buf[9];
+ return pos ? r : -r;
+ }
+ if (r > 214748364) {
+ return 0;
+ }
+ if (pos) {
+ if (buf[9] > 7) {
+ return 0;
+ }
+ } else {
+ if (buf[9] > 8) {
+ return 0;
+ }
+ }
+ r = r * 10 + buf[9];
+ return pos ? r : -r;
+}
+
+int main() {
+ printf("%d\n", reverse(123));
+ printf("%d\n", reverse(-123));
+} \ No newline at end of file