hdu 1002 A + B Problem II 高精度加法

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1002

高精度模板题

#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()
{
    int t,k=1;
    cin >> t;
    string a, b;
    while (t--){
        cin >> a >> b;
        cout << "Case " << k++ << ":" << endl;
        cout << a << " + " << b << " = ";
        cout << add(a, b) << endl;
        if (t != 0)
            cout << endl;
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/looeyWei/p/10502265.html
今日推荐