Autumn is a Genius
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 6040 | Accepted: 1269 |
Description
Jiajia and Wind have a very cute daughter called Autumn. She is so clever that she can do integer additions when she was just 2 years old! Since a lot of people suspect that Autumn may make mistakes, please write a program to prove that Autumn is a real genius.
Input
The first line contains a single integer T, the number of test cases. The following lines contain 2 integers A, B(A, B < 32768) each. The size of input will not exceed 50K.
Output
The output should contain T lines, each with a single integer, representing the corresponding sum.
Sample Input
1
1 2
Sample Output
3
Hint
There may be '+' before the non-negative number! 題目大意:Autumn是一個天才,他能計算A+B的和 (A, B < 32768)....... 思路:這個題玩文字遊戲,隻給定了AB的上限而沒給定AB下線,靠的。。。Autumn真是個天才,哎無奈 乖乖的用高精加減吧。。。汗 #include<iostream>
using namespace std;
char a[100000],b[100000],c[1000000];int lena,lenb,lenc;
void plus()
{
int jin=0,i;
lena=strlen(a)-1;
lenb=strlen(b)-1;
if(lena>lenb)
{ strcpy(c,a); lenc=lena; }
else
{ strcpy(c,b); strcpy(b,a); lenc=lenb; lenb=lena; }
while(lenb>-1)
{
i=c[lenc]+b[lenb]+jin-96;
c[lenc]=i%10+48;
jin=i/10;
lenc--; lenb--;
}
while(lenc>-1&&jin)
{
i=c[lenc]-48+jin;
c[lenc]=i%10+48;
jin=i/10;
lenc--;
}
if(jin)
{
for(i=strlen(c)+1;i>0;i--)
c[i]=c[i-1];
c[0]=jin+48;
}
}
void jian()
{
int jie=0,i,j;
lena=strlen(a)-1;
lenb=strlen(b)-1;
lenc=lena;
strcpy(c,a);
while(lenb>-1)
{
if(c[lenc]-b[lenb]-jie<0)
{
c[lenc]=c[lenc]-b[lenb]-jie+58;
jie=1;
}
else
{ c[lenc]=c[lenc]-b[lenb]-jie+48; jie=0; }
lenb--;lenc--;
}
while(lenc>-1&&jie)
{
if(c[lenc]-48-jie<0)
{ c[lenc]='9'; jie=1; }
else
{ c[lenc]-=jie; jie=0;}
lenc--;
}
for(i=0;c[i]=='0'&&i<strlen(c)-1;i++);
for(j=0;;j++)
{
c[j]=c[i+j];
if(c[j]=='/0')
break;
}
}
int main()
{
int t,i;
cin>>t;
while(t--)
{
int f1=1,f2=1;
cin>>a;
if(a[0]=='-'||a[0]=='+')
{
if(a[0]=='-')
f1=-1;
for(i=0;a[i]!='/0';i++)
a[i]=a[i+1];
}
cin>>b;
if(b[0]=='-'||b[0]=='+')
{
if(b[0]=='-')
f2=-1;
for(i=0;b[i]!='/0';i++)
b[i]=b[i+1];
}
if(f1*f2==1)
{
plus();
if(f1==-1)
cout<<'-'<<c<<endl;
else
cout<<c<<endl;
}
else if(f1*f2==-1)
{
if(f1==1)
{
if(strlen(a)>strlen(b)||(strlen(a)==strlen(b)&&strcmp(a,b)>0))
{
jian();
cout<<c<<endl;
}
else
{
strcpy(c,a); strcpy(a,b); strcpy(b,c);
jian();
if(strcmp(c,"0"))
cout<<'-'<<c<<endl;
else
cout<<"0"<<endl;
}
}
else
{
if(strlen(a)>strlen(b)||(strlen(a)==strlen(b)&&strcmp(a,b)>0))
{
jian();
if(strcmp(c,"0"))
cout<<'-'<<c<<endl;
else
cout<<"0"<<endl;
}
else
{
strcpy(c,a); strcpy(a,b); strcpy(b,c);
jian();
cout<<c<<endl;
}
}
}
}
return 0;
}