给出2个大整数A,B,计算A+B的结果。
Input
第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数)
Output
输出A + B
Sample Input
68932147586
468711654886
Sample Output
537643802472
C++版本的,我分了三种情况1:全正,2:全负,3:一正一负,看起来稍微有点长:
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
string Plus(string a,string b){
string temp="",ans=""; //temp是可能带前缀0的半成品 ,ans是返回结果
int *A,*B; //数字数组
if(a[0]!='-'&&b[0]!='-'){
if(a.length()<b.length()){
swap(a,b);
}
int n=a.length();
int m=b.length();
A=new int[n+1];
A[0]=0;
for(int i=0;i<n;i++){
A[i+1]=a[i]-'0';
}
B=new int[n+1];
int t=n-m+1;
for(int i=0;i<t;i++){
B[i]=0;
}
for(int i=t;i<=n;i++){
B[i]=b[i-t]-'0';
}
for(int i=n;i>=0;i--){
A[i]+=B[i];
if(A[i]>9){
A[i]-=10;
A[i-1]++;
}
}
if(A[0]!=0)
temp+=(A[0]+'0');
for(int i=1;i<=n;i++){
temp+=(A[i]+'0');
}
}
else if(a[0]=='-'&&b[0]=='-'){ //a,b都是负数,直接模拟加法
temp+='-';
if(a.length()<b.length()){
swap(a,b);
}
int n=a.length();
int m=b.length();
A=new int[n];
A[0]=0;
for(int i=1;i<n;i++){
A[i]=a[i]-'0';
}
B=new int[n];
int t=n-m+1;
for(int i=0;i<t;i++){
B[i]=0;
}
for(int i=t;i<n;i++){
B[i]=b[i-t+1]-'0';
}
for(int i=n-1;i>=0;i--){
A[i]+=B[i];
if(A[i]>9){
A[i]-=10;
A[i-1]++;
}
}
if(A[0]!=0)
temp+=(A[0]+'0');
for(int i=1;i<n;i++){
temp+=(A[i]+'0');
}
}
else{
if(b[0]=='-')
swap(a,b); //a[]负数,b[]正数。
string c="";
int mm=a.length();
int n=b.length();
for(int i=1;i<mm;i++){
c+=a[i];
} //取出a[]的数字 ,去掉负号
int m=c.length();
if(m<n){ //b的绝对值大
B=new int[n];
A=new int[n];
for(int i=0;i<n;i++){
B[i]=b[i]-'0';
}
int t=n-m;
for(int i=0;i<t;i++){
A[i]=0;
}
for(int i=t;i<n;i++){
A[i]=c[i-t]-'0';
}
for(int i=n-1;i>=0;i--){
B[i]-=A[i];
if(B[i]<0){
B[i]+=10;
B[i-1]--;
}
}
if(B[0]!=0){
temp+=(B[0]+'0');
}
for(int i=1;i<n;i++){
temp+=(B[i]+'0');
}
}
else if(m>n){ //m是c的长度 / n是b的长度
A=new int[m];
B=new int[m];
temp+='-';
for(int i=0;i<m;i++){
A[i]=c[i]-'0';
}
int t=m-n;
for(int i=0;i<t;i++){
B[i]=0;
}
for(int i=t;i<m;i++){
B[i]=b[i-t]-'0';
}
for(int i=m-1;i>=0;i--){
A[i]-=B[i];
if(A[i]<0){
A[i]+=10;
A[i-1]--;
}
}
if(A[0]!=0){
temp+=(A[0]+'0');
}
for(int i=1;i<m;i++){
temp+=(A[i]+'0');
}
}
else if(m==n){ //c,b绝对值长度一样 即m==n
if(c>b) temp+='-'; //负数绝对值大,temp肯定是负
if(c<b) swap(c,b); //保持c是绝对值大的 ,这样做只是为了求他俩的差值
A=new int[n]; //c==b的情况也放进来算了,只不过结果会是0000....
B=new int[n];
for(int i=0;i<n;i++){
A[i]=c[i]-'0';
B[i]=b[i]-'0';
}
for(int i=n-1;i>=0;i--){
A[i]-=B[i];
if(i>=1&&A[i]<0){ //第一位就不用判断了,最小也只可能是0
A[i]+=10;
A[i-1]--;
}
}
if(A[0]!=0){
temp+=(A[0]+'0');
}
for(int i=1;i<n;i++){
temp+=(A[i]+'0');
}
}
}
delete []A; //之前的处理结束 ,temp保存了一个带前缀0,带符号的串
delete []B; //切记要归还内存
int cnt=-1,k=temp.length(); //这个cnt是用来找temp中第一个不为0的数字的下标的
if(temp[0]=='-'){
ans+='-';
for(int i=1;i<k;i++){
if(temp[i]!='0')
{
cnt=i;
break;
}
}
}
else for(int i=0;i<k;i++){
if(temp[i]!='0'){
cnt=i;
break;
}
}
if(cnt== -1){ //cnt还是初始值,说明扫完temp发现它的数字部分全部都是0
ans.clear();
ans+='0';
}
else{
for(int i=cnt;i<k;i++){
ans+=temp[i]; //不全部为0,就从第一个不为零的地方存进ans
}
}
return ans;
}
int main(){
string a,b;
while(cin>>a>>b){
cout<<Plus(a,b)<<endl;
}
return 0;
}