华中科技大学机试 a+b Easy *大数加法问题
作者:互联网
基本思路:
详见机试题目总结专栏;
关键点:
无;
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; const int maxn = 1000; struct bign { int len; int num[maxn]; bign() { fill(num, num + maxn, 0); len = 0; } }; bign trans(string s) { bign a; for (int i = s.size() - 1; i >= 0; i--) { a.num[a.len++] = (s[i] - '0'); } return a; } 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.num[i] + b.num[i] + carry; c.num[c.len++] = temp % 10; carry = temp / 10; } if (carry != 0) { c.num[c.len++] = carry; } return c; } string bign2string(bign a) { string s = ""; for (int i = a.len - 1; i >= 0; i--) { s += char(a.num[i] + '0'); } return s; } int main() { string s1, s2; while (cin>>s1>>s2){ bign a = trans(s1); bign b = trans(s2); bign c = add(a, b); cout << bign2string(c) << endl; } return 0; }
标签:int,华中科技大学,len,num,bign,Easy,carry,机试,include 来源: https://www.cnblogs.com/songlinxuan/p/12418923.html