# 51nod 大数相加（含负数）

Input

Output

Input示例

68932147586 468711654886

Output示例

537643802472

#include <iostream>
using namespace std;
string A,B;
string jia(string a,string b)
{
long long len1=a.size(),len2=B.size();
if(a.size()<b.size())
{
swap(a,b);
swap(len1,len2);
}
int x,y,flag=0,sum;
while(len1>0)
{
x=a[len1-1]-'0';
if(len2>0)
y=b[len2-1]-'0';
else y=0;
sum=x+y+flag;
if(sum>=10)
{
flag=1;
a[len1-1]=sum%10+'0';
}
else
{
flag=0;
a[len1-1]=sum%10+'0';
}
len1--;
len2--;
}
if(flag==1) a="1"+a;
return a;
}
string jian(string a,string b)
{
long long len1=a.size(),len2=b.size();
int x,y,sum,flag=0;
while(len1>0)
{
x=a[len1-1]-'0';
if(len2>0) y=b[len2-1]-'0';
else y=0;
sum=x-y+flag;
if(sum<0)
{
flag=-1;
a[len1-1]=(10+sum)+'0';
}
else
{
a[len1-1]=sum+'0';
flag=0;
}
len1--;
len2--;
}
int t=0;
while(a[t]=='0')
{
a.erase(0,1);
}
if(a.size()==0) a="0";
return a;
}
int main()
{
ios::sync_with_stdio(false);
cin>>A>>B;
if(A[0]!='-'&&B[0]!='-')
{
cout<<jia(A,B)<<endl;
}
else if(A[0]=='-'&&B[0]=='-')
{
A.erase(0,1);
B.erase(0,1);
cout<<"-"<<jia(A,B)<<endl;
}
else if(A[0]=='-'&&B[0]!='-')
{
A.erase(0,1);
int t1=A.size(),t2=B.size();
if(t1>t2)
{
cout<<"-"<<jian(A,B)<<endl;
}
else if(t1<t2)
{
cout<<jian(B,A)<<endl;
}
else if(t1==t2)
{
if(A<=B) cout<<jian(B,A)<<endl;
else if(A>B) cout<<"-"<<jian(A,B)<<endl;
}
}
else if(A[0]!='-'&&B[0]=='-')
{
B.erase(0,1);
int t1=A.size(),t2=B.size();
if(t1>t2)
{
cout<<jian(A,B)<<endl;
}
else if(t1<t2)
{
cout<<"-"<<jian(B,A)<<endl;
}
else if(t1==t2)
{
if(A>=B) cout<<jian(A,B)<<endl;
else if(A<B) cout<<"-"<<jian(B,A)<<endl;
}
}
return 0;
}



• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120