大数相加模板

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <numeric>
#define maxn 101
//char a[maxn],b[maxn],sum[maxn];
using namespace std;
string sum(string s1,string s2)
{
    if(s1.length()<s2.length())
    {
        string temp=s1;
        s1=s2;
        s2=temp;
    }
    int i,j;
    for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
    {
        s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));   //注意细节
        if(s1[i]-'0'>=10)
        {
            s1[i]=char((s1[i]-'0')%10+'0');
            if(i) s1[i-1]++;
            else s1='1'+s1;
        }
    }
    return s1;
}
int main()
{
    string a,b;
    while(cin>>a>>b)
    {
        if(a[0]=='0'&&b[0]=='0') break;
        cout<<sum(a,b)<<endl;
    }
    return 0;
}

  

posted @ 2017-08-09 11:03  Roni_i  阅读(111)  评论(0编辑  收藏  举报