首页 > 代码库 > A + B problem 高精度

A + B problem 高精度

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;
    int i1 = a.size() - 1;
    int i2 = b.size() - 1;
    string s;
    int carry = 0;
    while (i1 >= 0 || i2 >= 0)
    {
        char ch = carry;
        if (i1 >= 0)
        {
            if (a[i1] < 0 || a[i1] > 9)
                continue;
            ch += a[i1] - 0;
        }
        if (i2 >= 0)
        {
            if (b[i2] < 0 || b[i2] > 9)
                continue;
            ch += b[i2] - 0;
        }
        if (ch >= 10)
        {
            carry = 1;
            ch -= 10;
        }
        else carry = 0;
        s.push_back(ch + 0);
        i1--;
        i2--;
    }
    if (carry) 
    s.push_back(1);
    reverse(s.begin(), s.end());
    return s;
}

 

A + B problem 高精度