前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >51Nod--1005 大数加法

51Nod--1005 大数加法

作者头像
指点
发布2019-01-18 15:36:24
4290
发布2019-01-18 15:36:24
举报
文章被收录于专栏:指点的专栏指点的专栏

题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1005

基准时间限制:1 秒 空间限制:131072 KB 分值: 0

给出2个大整数A,B,计算A+B的结果。 Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Input示例 68932147586 468711654886 Output示例 537643802472

其实就是模拟我们在草稿纸上进行加减运算的过程,关键点在代码中给出了注释,代码使用了一些面向对象的性质:

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <cmath>
using namespace std; 

struct BigNumber {
    // 初始化,处理负数 
    void init() {
        if(this->str[0] == '-') {
            isPos = false;
            this->str.erase(this->str.begin(), this->str.begin()+1);
        } else {
            isPos = true;
        }
        length = str.length();
    }
    // 两个大整数相加的函数 
    string &add(BigNumber &other) {
        // 在位数短的那一个数前面补零,使两者位数相等 
        int diff = abs(other.length - this->length);
        other.length > this->length ? this->str.insert(0, diff, '0') : other.str.insert(0, diff, '0');
        // 两个正数或者两负,两负,就是两正数相加,符号为负 
        if(this->isPos && other.isPos || !this->isPos && !other.isPos) {
            for(int i = str.length()-1; i >= 0; i--) {
                str[i] += other.str[i] - '0';
                // 如果和大于字符 9,即为 10 以上,那么进位 
                if(str[i] > '9') {
                    // 如果是左边第一位数,那么注意处理进位 
                    if(i == 0) {
                        str.insert(0, 1, (str[i]-'0')/10+'0');
                        str[1] = '0' + (str[1]-'0')%10;
                    } else {
                        str[i-1] += (str[i]-'0')/10;
                        str[i] = '0' + (str[i]-'0')%10;
                    }
                    str[i] = '0' + (str[i]-'0')%10;
                }
            }
            // 一正一负相加,即为两数绝对值相减,符号为绝对值大的那个 
        } else {
            // 找出两个中绝对值较大的那个,如果不是this->str,那么交换 
            for(int i = 0; i < str.length(); i++) {
                if(str[i] < other.str[i]) {
                    this->str.swap(other.str);
                    // 更新结果正负 
                    this->isPos = other.isPos;
                    break;
                } else if(str[i] > other.str[i]) {
                    break;
                }
            }
            for(int i = str.length()-1; i >= 0; i--) {
                str[i] -= other.str[i] - '0';
                // 不够减则向左边一位数借 1  
                if(str[i] < '0') {
                    str[i] += 10;
                    str[i-1]--;
                }
            }
        }
        // 清除结果前面多余的 0  
        int zeroNum;
        for(zeroNum = 0; zeroNum < str.length(); zeroNum++) {
            if(str[zeroNum] != '0') {
                break;
            }
        }
        str.erase(str.begin(), str.begin()+zeroNum);
        // 如果结果是负数,那么开头加负号 
        if(!this->isPos) {
            this->str.insert(0, 1, '-');
        }
        // 还需要注意的是如果本身答案就是 0 的情况 
        if(str.length() == 0) {
            str.append(1, '0');
        }
        return str;
    }

    string str;
    int length;
    // 是否为正数 
    bool isPos;
};


int main() {
    BigNumber a, b;
    cin >> a.str >> b.str;
    a.init();
    b.init();

    cout << a.add(b) << endl;

    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年06月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档