From b298f6a131b6412cdecba6317dc00e6ef5f46aca Mon Sep 17 00:00:00 2001 From: Mistivia Date: Mon, 13 Oct 2025 18:41:46 +0800 Subject: solve 7 --- 0007/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0007/main.c 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 +#include +#include +#include +#include + +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 -- cgit v1.0