题解 | #高精度整数加法#
高精度整数加法
http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include<iostream>
#include<stack>
using namespace std;
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
string res;
stack<char>st;
int l1=s1.size();
int l2=s2.size();
int i=l1-1,j=l2-1,up=0;
while(i>=0&&j>=0)
{
int s=s1[i]-'0'+s2[j]-'0'+up;
up=s/10;
int y=s%10;
st.push(y+'0');
i--;
j--;
}
while(i>=0)
{
int s=s1[i]-'0'+up;
up=s/10;
int y=s%10;
st.push(y+'0');
i--;
}
while(j>=0)
{
int s=s2[j]-'0'+up;
up=s/10;
int y=s%10;
st.push(y+'0');
j--;
}
if(up>0)
{
st.push(up+'0');
}
while(!st.empty())
{
res.push_back(st.top());
st.pop();
}
cout<<res<<endl;
}
}