C/C++ 超长正整数相加

本题整体思想不难,主要是想好用什么容器去存储计算值和计算结果值,这里用的是字符串,当然也可以用数组,题目链接:https://www.nowcoder.com/practice/5821836e0ec140c1aa29510fd05f45fc?tpId

以下为解答代码(具体细节看注释):

string AddLongInteger(string addend, string augend){
    int i = 0,n=addend.size()>augend.size()?n=addend.size():n=augend.size();  //n为较长计算值的长度,用来循环计算时使用
    string c;              //计算结果保存的值
    int  temp, tep = 0;         //进位值要记得初始化
    reverse(addend.begin(), addend.end());         //这里将两个加数都翻转过来计算,主要是为了写入结果的时候可以直接使用‘+=’
    reverse(augend.begin(), augend.end());         //当然也可以没有这一步,直接从后往前算
    for (; i < n; i++){
        int a = i<addend.size() ? addend[i] - '0' : 0;         //若是一个加数已经全部计算完成,在接下来就用0代替来计算
        int b = i<augend.size() ? augend[i] - '0' : 0;
        temp = (a+b+tep)% 10;           //计算结果值(要填入结果的值)
        tep = (a + b + tep) / 10;   //进位值
        c += temp + 48;  // 填入结果
    }
    if (tep>0){    //若是最后一位计算有进位值,则直接填入结果
        c += tep+'0';
    }
    reverse(c.begin(), c.end());   //将计算结果反过来就是正确结果
    return c;
}

int main(){
    string a, b, c;
    while (cin >> a >> b){
        cout << AddLongInteger(a, b) << endl;
    }
    return 0;
}
posted @ 2019-12-21 16:43  Kaniso_Vok  阅读(158)  评论(0编辑  收藏  举报