题解 | #高精度整数加法#

高精度整数加法

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;
    }
}
全部评论

相关推荐

让资本家给我当牛做马:26的秋招还没开始啊?你找的是实习?实习的话你马上就研三了为什么还要实习?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务