I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2 1 2 112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
注: 提交多次格式错误,特别注意cnt的数据,它的初始数据为1,cnt代表第几组,当cnt!=T的时候,就代表要多输出一行空白,所以要多一步判断,更加要注意的是cnt++的位置,很容易出错。(本人又一次因为演示错误耽误了半小时)
纯纯水题
思路:
将其转化为字符串,进行模拟数字进行加减法就行
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
//int c1[10000],c2[10000];//万进制数组
void cf(char *a1,char *a2){
int l1=strlen(a1);
int l2=strlen(a2);
//cout<<l1<<" "<<l2<<endl;
int w;
if(l1>=l2){
w=l2;
}
else{
char *b;
b=a2;
a2=a1;
a1=b;
w=l1;
}
l1=strlen(a1);
l2=strlen(a2);
//printf("%s %s\n",a1,a2);
//cout<<l1<<" "<<l2<<endl;
for(int i=0;i<w;i++){
a1[l1-i-1]+=a2[l2-i-1]-'0';
if(a1[l1-i-1]>'9'){
a1[l1-i-1]-=10;
if((l1-i-1)!=0)
a1[l1-i-2]+=1;
else{
char *t=new char[1000];
t[0]='1';
for(int s=0;s<l1;s++){
t[s+1]=a1[s];
}
a1=t;
l1++;
}
}
}
for(int i=0;i<l1;i++){
cout<<a1[i];
}
cout<<endl;
//cout<<endl;
return;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cout<<"Case "<<i+1<<":"<<endl;
char *a1;
a1=new char[1000];
a1[0]='0';
char *a2;
a2=new char[1000];
a2[0]='0';
scanf("%s",a1);
scanf("%s",a2);
// cout<<"Case "<<i+1<<":"<<endl;
printf("%s + %s = ",a1,a2);
cf(a1,a2);
// if(1){
// cout<<endl;
// cout<<endl;}
// if(i!=n){
// cout<<endl;
// }
//cout<<endl;
if(i!=n-1){
cout<<endl;
}
}
return 0;
}
//附带一个示例
//1416546432123165484
//13146545613213214564654