看清格式描述很重要:
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.
如1 + 2 = 3;有几个空格,两个测试用例之间输入一个空行;
本人就是改了个presentation error改了好久......
C++:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool check(string a,string b);
void StringPlus(string a,string b);
int main()
{
int T=0;
string a;
string b;
cin>>T;
if(T>20||T<1)return 0;
int count=1;
while(T!=0&&cin>>a>>b&&!cin.fail())
{
bool Istrue=check(a,b);
if(Istrue)
{
cout<<"Case "<<count<<":"<<endl;
StringPlus(a,b);
cout<<endl;
}
T--;
count++;
if(T>0)cout<<endl;
}
int Pause;
cin>>Pause;
return 0;
}
bool check(string a,string b)
{
int i=0;
int j=0;
for(i;i<a.length();i++)
{
if(a[i]<'0'||a[i]>'9')break;
}
for(j;j<b.length();j++)
{
if(b[j]<'0'||b[j]>'9')break;
}
if(i<a.length()||j<b.length())return false;
return true;
}
void StringPlus(string a,string b)
{
vector<char> temp;
string L,S;
if(a.length() >b.length() )
{
L=a;
S=b;
}
else{
L=b;
S=a;
}
int i,j;
int temp1,temp2;
char temp3;
int add=0;//进位
int yushu;
for(i=S.length()-1,j=L.length ()-1;i>=0;i--,j--)
{
temp1=S[i]-48;
temp2=L[j]-48;
//cout<<"temp1 temp2 add:"<<temp1<<" "<<temp2<<" "<<add<<endl;
yushu=add+temp1+temp2-10;
//cout<<"yushu:"<<yushu<<endl;
if(yushu>=0)add=1;
else {yushu+=10;add=0;}//此处add置0很重要
//cout<<"yushu"<<yushu<<endl;
temp3=yushu+48;
temp.push_back(temp3);
}
for(j;j>=0;j--)
{
temp2=L[j]-48;
yushu=add+temp2-10;
if(yushu>=0)add=1;
else {yushu+=10;add=0;}//此处add置0很重要
temp3=yushu+48;
temp.push_back(temp3);
}
if(add==1){temp3=add+48;temp.push_back(temp3);}
cout<<a<<" + "<<b<<" = ";
for(int k=temp.size()-1;k>=0;k--)
{
cout<<temp[k];
}
}
C:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void change(char *a)
{
int l,i;
char temp;
l=strlen(a);
for(i=0;i<l/2;i++)
{
temp=a[i];
a[i]=a[l-1-i];
a[l-1-i]=temp;
}
}
void Add(char *a,char *b)
{
int lena,lenb,i,max;
int aa[1001],bb[1001];
lena=strlen(a);
lenb=strlen(b);
max=(lena>lenb?lena:lenb);
memset(aa,0,sizeof(aa));
memset(bb,0,sizeof(bb));
for(i=0;i<lena;i++)
aa[i]=a[i]-'0';
for(i=0;i<lenb;i++)
bb[i]=b[i]-'0';
for(i=0;i<=max;i++)
{
aa[i]=aa[i]+bb[i];
if(aa[i]>9)
{
aa[i]-=10;
aa[i+1]+=1;
}
}
if(aa[max]>0)
printf("%d",aa[max]);
for(i=max-1;i>=0;i--)
{
printf("%d",aa[i]);
}
printf("\n");
}
int main()
{
char a[1001],b[1001];
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s%s",a,b);
printf("Case %d:\n",i);
printf("%s + %s = ",a,b);
change(a);
change(b);
Add(a,b);
if(i<n)
printf("\n");
}
//system("pause");
return 0;
}