题解 | #a+b#
a+b
https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4
#include<stdio.h> #include<string.h> char str1[1001], str2[1001]; int a[1001], b[1001]; int convert(char str[], int num[]) { //将字母转变为数字,按照低位到高位存放 int len = strlen(str); for (int i = 0; i < len; i++) num[i] = str[len - i - 1] - '0'; return len; } int main() { int n1, n2, n; while (scanf("%s%s", str1, str2) != EOF) { int flag = 0; //数组置0,flag为进位 n1 = convert(str1, a); n2 = convert(str2, b); n = n1 >= n2 ? n1 : n2; for (int i = 0; i < n; i++) { int temp = (a[i] + b[i] + flag) / 10; //商为进位,余数为当前位结果 a[i] = (a[i] + b[i] + flag) % 10; flag = temp; } if (flag) a[n++] = flag; //最高位产生进位 for (int i = n - 1; i >= 0; i--) printf("%d", a[i]); printf("\n"); } return 0; }