题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <stdio.h> #include <string.h> #include <time.h> int main() { char n1[10010] = {0}; char n2[10010] = {0}; char answer[10010] = {0}; gets(n1); gets(n2); int tmp = 0; int p1 = strlen(n1) - 1; int p2 = strlen(n2) - 1; int p_answer = 0; for(; p1 >= 0 && p2 >= 0; --p1, --p2) { //加上进位 tmp = (n1[p1]-'0') + (n2[p2]-'0') + tmp; if(tmp >= 10) { answer[p_answer++] = tmp % 10 + '0'; tmp = 1; //往前进一 } else { answer[p_answer++] = tmp + '0'; tmp = 0; } } while(p1 >= 0) { tmp = n1[p1] - '0' + tmp; if(tmp >= 10) { answer[p_answer++] = tmp % 10 + '0'; tmp = 1; } else { answer[p_answer++] = tmp + '0'; tmp = 0; } -- p1; } while(p2 >= 0) { tmp = n2[p2] - '0' + tmp; if(tmp >= 10) { answer[p_answer++] = tmp % 10 + '0'; tmp = 1; } else { answer[p_answer++] = tmp + '0'; tmp = 0; } -- p2; } if(tmp > 0) { answer[p_answer ++] = tmp + '0'; } for(-- p_answer; p_answer >= 0; -- p_answer) { putchar(answer[p_answer]); } return 0; }