题解 | #a+b#
a+b
https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4
#include <iostream> #include <cstring> using namespace std; struct bign { int d[1000]; int len; bign() { len = 0; memset(d, 0, sizeof(d)); } }; bign add(bign a, bign b) { bign c; int carry = 0;//进位 for (int i = 0; i < a.len || i < b.len; i++) { int temp = a.d[i] + b.d[i] + carry; c.d[c.len++] = temp % 10; carry = temp / 10; } if (carry != 0) { c.d[c.len++] = carry; } return c; } bign change(char s[]) { bign c; string str = s; c.len = str.size(); for (int i = 0; i < c.len; i++) { //倒着赋值 c.d[i] = str[c.len - i - 1] - '0'; } return c; } void print(bign a) { for (int i = a.len - 1; i >= 0; i--) { printf("%d", a.d[i]); } printf("\n"); } int main() { char s1[1000]; char s2[1000]; scanf("%s", s1); scanf("%s", s2); bign a = change(s1); bign b = change(s2); bign c = add(a, b); print(c); return 0; }