题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <stdio.h> #include <string.h> int main() { char str1[101], str2[101], result[102] = {0}; int carry = 0, i, j, k, len1, len2, sum; scanf("%s %s", str1, str2); len1 = strlen(str1); len2 = strlen(str2); i = len1 - 1; j = len2 - 1; k = 0; while (i >= 0 || j >= 0) { sum = carry; if (i >= 0) sum += str1[i--] - '0'; if (j >= 0) sum += str2[j--] - '0'; carry = sum / 10; result[k++] = sum % 10 + '0'; } if (carry) { result[k++] = carry + '0'; } for (i = k - 1; i >= 0; i--) { printf("%c", result[i]); } printf("\n"); return 0; }