题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <stdio.h> #include <string.h> int main() { char a[10000], b[10000]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); scanf("%s",a); scanf("%s",b); /**逆序字符串*/ int cntStr = strlen(a)%2==0?strlen(a)/2:(strlen(a)-1)/2; for(int i=0;i<cntStr;i++){ char temp = a[strlen(a)-i-1]; a[strlen(a)-i-1] = a[i]; a[i] = temp; } cntStr = strlen(b)%2==0?strlen(b)/2:(strlen(b)-1)/2; for(int i=0;i<cntStr;i++){ char temp = b[strlen(b)-i-1]; b[strlen(b)-i-1] = b[i]; b[i] = temp; } // printf("%s\n", a); // printf("%s\n", b); /*相加**/ cntStr = strlen(b) > strlen(a)?strlen(b):strlen(a); int flag = 0; for(int i=0;i<cntStr;i++){ int aNumber = a[i] >= '0' ? a[i]-'0':a[i]; int bNumber = b[i] >= '0' ? b[i]-'0':b[i]; if(aNumber + bNumber + flag >=10){ a[i] = aNumber + bNumber + flag - 10 + '0'; flag = 1; }else { a[i] = aNumber + bNumber + flag + '0' ; flag = 0; } } if(flag) { a[cntStr] = '1'; a[cntStr+1] = '\0'; } for(int i=strlen(a)-1;i>=0;i--){ printf("%c",a[i]); } return 0; }