高精度加法模板题
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> using namespace std; #define _for(i, a, b) for(int i = a; i < b; ++i) struct bign{ int len; int data[1010]; bign(){ len = 0; memset(data, 0, sizeof(data)); } }; bign change(string a){ bign c; int lena = a.length(); c.len = lena; _for(i, 0, lena){ c.data[i] = a[lena - i - 1] - '0'; } return c; } bign add(bign a, bign b){ bign c; int len = max(a.len, b.len); int carry = 0; _for(i, 0, len){ int tmp = a.data[i] + b.data[i] + carry; c.data[c.len++] = tmp % 10; carry = tmp / 10; } if(carry != 0){ c.data[c.len++] = carry; } return c; } void print(bign a){ for(int i = a.len - 1; i >= 0; --i){ printf("%d", a.data[i]); } printf("\n"); } int main(){ string s1, s2; while(cin >> s1 >> s2){ bign a = change(s1), b = change(s2); print(add(a, b)); } return 0; }