首页 > 其他 > 详细

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

时间:2019-03-09 18:09:01      阅读:158      评论:0      收藏:0      [点我收藏+]

题目链接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

 

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

原文:https://www.cnblogs.com/looeyWei/p/10502265.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!