A + B Problem II
计算两个1000位以内整数的和
输入
2
1 2
112233445566778899 998877665544332211
输出
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
#include <iostream>
using namespace std;
int main()
{
int T, count_T;
char a[1001], b[1001], ans[1002];
cin >> T;
for(count_T=1; count_T <= T; count_T++) {
cin >> a >> b;
if(count_T==1) {
cout << "Case " << count_T << ":" << endl;
}
else {
cout << "\nCase " << count_T << ":" << endl;
}
cout << a << " + " << b << " = ";
int la=strlen(a)-1, lb=strlen(b)-1, sign=0, i;
for(i=0;la>=0 && lb>=0; la--,lb--,i++) {
sign += a[la] + b[lb] - '0' - '0';
ans[i] = sign % 10;
sign /= 10;
}
for(; la>=0; la--, i++) {
sign += a[la] - '0';
ans[i] = sign % 10;
sign /= 10;
}
for(; lb>=0; lb--, i++) {
sign += b[lb] - '0';
ans[i] = sign % 10;
sign /= 10;
}
if(sign) {
ans[i] = sign;
i++;
}
for(i--; i>=0; i--) {
cout << (char)(ans[i] + '0');
}
cout << endl;
}
return(0);
}
posted on 2008-02-12 15:38
幽幽 阅读(990)
评论(2) 编辑 收藏 引用 所属分类:
HDOJ日志