大数运算 A + B Problem II
http://acm.hdu.edu.cn/showproblem.php?pid=1002
代码
#include<iostream> #include<string> #include<cstring> #include<algorithm> struct bignum { int list[2000]; int len; bignum() { memset(list, 0, sizeof(list)); len =0; } }aa,bb,ans; using namespace std; bignum change(string a) { bignum tmp; tmp.len = a.length(); for (int i = 0; i < a.length(); i++) tmp.list[i] = a[tmp.len - i-1]-'0'; return tmp; } bignum add(bignum a, bignum b) { int t = 0,i; bignum ans; ans.len = a.len > b.len ? a.len : b.len; for ( i = 0; i < a.len ||i< b.len; i++) { ans.list[i] = (t + a.list[i] + b.list[i]) % 10; t= (t + a.list[i] + b.list[i]) / 10; } if (ans.list[i] > 10) { ans.list[i] = ans.list[i] % 10; ans.list[i + 1] /= 10; } return ans; } int main() { int n; string a, b; scanf("%d", &n); for (int k = 1; k <=n;k++) { printf("Case %d:\n", k); cin >> a >> b; cout << a << " + " << b << " = "; aa = change(a); bb = change(b); ans = add(aa, bb); for (int i = ans.len-1; i >= 0;i--) cout << ans.list[i]; cout << endl; if (k != n)cout << endl; } }