SDNUOJ 1303 加法高精度

Description
求A+B

Input
多组测试样例。两个正整数X,Y(0≤X,Y≤10^100)
Output
输出结果
Sample Input
1 1
12345 54321
Sample Output
2
66666

开始没看到要多组输入样例,郁闷了一会儿…

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;

int main()
{
    char a[105] = {}, b[105] = {}, c[105] = {};
    while(cin >> a)
    {
        cin >> b;
        int lena = strlen(a);
        int lenb = strlen(b);
        ///确保 a 是较大(较长)的数字
        if(lena < lenb)
        {
            for(int i = 0; i < lenb; i++)
            {
                swap(a[i], b[i]);
            }
            swap(lena, lenb);
        }
        reverse(a, a+lena);
        reverse(b, b+lenb);
//    cout << a << '\n' << b << '\n';
        ///逢十所进
        int j = 0;
        int i;
        for(i = 0; i < lenb; i++)
        {
            c[i] = (a[i] + b [i] - '0' - '0' + j)%10 + '0';
            j = (a[i] + b[i] - '0' - '0' + j)/10;
        }
        for(int k = i; k < lena; k++)
        {
            ///照顾衔接处及末(反转后)
            c[k] = (a[k] + j - '0' )%10 + '0';
            j = (a[k] + j - '0' )/10;
        }
        if(j != 0)
            cout << j;
        for(int m = lena - 1; m >= 0; m--)
            cout << c[m];
        cout << '\n';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhaobaole2018/article/details/84647233
今日推荐