#include <stdio.h> #include <string.h> #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif int main(void) { static char a [5000 + 1]; static char b [sizeof(a)]; static char c [sizeof(a) + 1]; char * out = c + sizeof(c); size_t a_len, b_len; int carry = 0; if (scanf("%s %s", a, b)) { } a_len = strlen(a); b_len = strlen(b); *--out = '\0'; while (a_len || b_len) { int a_v = a_len ? a[--a_len] : '0'; int b_v = b_len ? b[--b_len] : '0'; carry += a_v - '0' + b_v - '0'; if (carry < 10) { *--out = '0' + carry; carry = 0; } else { *--out = '0' + carry - 10; carry = 1; } } if (carry) { *--out = '0' + carry; } puts(out); return EXIT_SUCCESS; } #ifdef __cplusplus } #endif
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <stdio.h> #include <string.h> #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif int main(void) { static char a [5000 + 1]; static char b [sizeof(a)]; static char c [sizeof(a) + 1]; char * out = c + sizeof(c); size_t a_len, b_len; int carry = 0; if (scanf("%s %s", a, b)) { } a_len = strlen(a); b_len = strlen(b); *--out = '\0'; while (a_len || b_len) { int a_v = a_len ? a[--a_len] : '0'; int b_v = b_len ? b[--b_len] : '0'; carry += a_v - '0' + b_v - '0'; if (carry < 10) { *--out = '0' + carry; carry = 0; } else { *--out = '0' + carry - 10; carry = 1; } } if (carry) { *--out = '0' + carry; } puts(out); return EXIT_SUCCESS; } #ifdef __cplusplus } #endif |