题解 | #高精度整数加法#
高精度整数加法
http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
少写一种情况!爷爷
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string str1,str2;
int temp = 0;
cin>>str1>>str2;
int size =str1.size();
int size2 = str2.size();
if(size>size2)
{
for(int i=0;i<size2;i++)
{
if(temp>=10)
temp = (int)(str1[size-1-i]-'0') +(int)(str2[size2-1-i]-'0') +1;
else
temp = (int)(str1[size-1-i]-'0') +(int)(str2[size2-1-i]-'0') ;
str1[size-1-i] = temp%10 +'0';
}
if(temp>=10)
str1[size-1-size2] = (int)(str1[size-1-size2])+1;
cout<<str1;
}
if(size == size2)
{
for(int i=0;i<size2;i++)
{
if(temp>=10)
temp = (int)(str1[size-1-i]-'0') +(int)(str2[size2-1-i]-'0') +1;
else
temp = (int)(str1[size-1-i]-'0') +(int)(str2[size2-1-i]-'0') ;
str1[size-1-i] = temp%10 +'0';
}
if(temp>=10)
str1.insert(str1.begin(),'1');
cout<<str1;
}
else if(size<size2)
{
for(int i=0;i<size;i++)
{
if(temp>=10)
temp = (int)(str2[size2-1-i]-'0') +(int)(str1[size-1-i]-'0') +1;
else
temp = (int)(str2[size2-1-i]-'0') +(int)(str1[size-1-i]-'0') ;
str2[size2-1-i] = temp%10 +'0';
}
if(temp>=10)
if(str2[size2-1-size]-'0'<9)
str2[size2-1-size] = (int)(str2[size2-1-size])+1;
else if(str2[size2-1-size]-'0'==9)
{
str2[size2-1-size] = '0';
str2[size2-2-size] = (int)(str2[size2-2-size])+1;
}
cout<<str2;
}
}

