原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
分析:列竖式!
代码:
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N = 1e3 + 7;
#define jiechufengyin std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;
string a, b;
ll ans[N], A[N], B[N];
int main(){
cin >> t;
for (int i = 1; i <= t; ++i){
cin >> a >> b;
memset(ans, 0, sizeof ans);
memset(A, 0, sizeof A);
memset(B, 0, sizeof B);
int lena = a.length();
int lenb = b.length();
for (int k = 0; k < lena; ++k){
A[lena - k - 1] = a[k] - '0';
}
for (int k = 0; k < lenb; ++k){
B[lenb - k - 1] = b[k] - '0';
}
int len = max(lena, lenb);
for (int k = 0; k < len; ++k){
ans[k] += A[k] + B[k];
if(ans[k] >= 10){
ans[k] -= 10;
ans[k + 1]++;
}
}
cout << "Case " << i << ":" << endl << a << " + " << b << " = ";
if(ans[len] != 0) cout << ans[len];
for (int k = len - 1; k >= 0; --k){
cout << ans[k];
}
cout << endl;
if(i != t) cout << endl;
}
return 0;
}