高精度

高精度加法

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int an[maxn], bn[maxn];
string add(string a, string b){
    memset(an, 0, sizeof an);
    memset(bn, 0, sizeof bn);
    for (int i = 0; i < a.length(); i++) an[i] = a[a.length() - i - 1] - '0';
    for (int i = 0; i < b.length(); i++) bn[i] = b[b.length() - i - 1] - '0';
    int len = max(a.length(), b.length());
    for (int i = 0; i < len; i++){
        an[i] += bn[i], an[i + 1] += an[i] / 10, an[i] %= 10;
    }
    if (an[len]) len++;
    string res = "";
    for (int i = 0; i < len; i++)
        res += an[len - 1 - i] + '0';
    return res;
}
int main()
{
    string a, b;
    cin >> a >> b;
    cout << a << " + " << b << " = ";
    cout << add(a, b) << endl;
    return 0;
}
View Code

 

posted @ 2019-03-09 18:30  looeyWei  阅读(295)  评论(0编辑  收藏  举报