#include <iostream>
#include<string.h>
using namespace std;
int BigAdd(int*a,int*b,int*ans){
ans[0] = a[0]>b[0]? a[0]:b[0];
//竖式相加
for (long i=1; i<=ans[0]; i++){
ans[i] = a[i] + b[i];
}
//进位
for (long i=1; i<=ans[0]; i++) {
if(ans[i]>9){
ans[i+1] += ans[i]/10;
ans[i] %= 10;
if(i==ans[0]) ans[0]++;
}
}
return 1;
}
void PrintAns(int*ans){
for (long i = ans[0]; i > 0; i--) {
cout << ans[i];
}
}
int main(){
long t,i,j,k;
k = 1;
cin>>t;
while(t--){
char sa[10005] = {0};
char sb[10005] = {0};
int na[10005] = {0}, nb[10005] = {0}, ans[10005] = {0};
cin>>sa>>sb;
na[0] = strlen(sa);
nb[0] = strlen(sb);
for(i=1,j=na[0]-1; i<=na[0];i++,j--){
na[i]=sa[j] - '0';
}
for(i=1,j=nb[0]-1; i<=nb[0];i++,j--){
nb[i]=sb[j] - '0';
}
BigAdd(nb,na,ans);
cout<<"Case "<<k<<":"<<"\n";
k++;
cout << sa << " + " << sb << " = ";
PrintAns(ans);
if(t) cout<<"\n\n";;
//cout<<"\n";
}
return 0;
}
- 注意每个案例前初始化变量,避免遗留数据影响
- 注意题目要求的输出格式
- 相关要点:大数相加