题解 | #a+b#
a+b
https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4
#include <stdio.h> #include <string.h> int main() { char a[1000], b[1000]; int result[1000] = {0}; while (scanf("%s %s", a, b) != EOF) { // 注意 while 处理多个 case // 64 位输出请用 printf("%lld") to //printf("%s-----%s\n",a,b); int i, j, aLen = strlen(a), bLen = strlen(b), carry = 0; //初始化处理一下输入的字符串 //右移动,考右边对齐 for(i=0,j=0;i<aLen||j<bLen;i++,j++){ if(i<aLen) a[999-i]=a[aLen-1-i]; if(j<bLen) b[999-j]=b[bLen-1-j]; } //左边空位清0,方便计算 for(i=0,j=0;i<1000-aLen||j<1000-bLen;i++,j++){ if(i<1000-aLen){ a[i]='0'; } if(j<1000-bLen){ b[j]='0'; } } for(i=0;i<1000;i++) result[i]=0; for (i = 1000 - 1; aLen > 0 || bLen > 0; i--) { result[i] = (a[i] - '0' + b[i] - '0' + carry) % 10; carry = (a[i] - '0' + b[i] - '0' + carry) / 10 ; aLen--; bLen--; } if(carry==1){ result[i--]=1; } for(i=i+1;i<1000;i++) { printf("%d",result[i]); } printf("\n"); } return 0; }