51Nod--1005 大数加法

来源:互联网 发布:淘宝2017年大股东是谁 编辑:程序博客网 时间:2024/06/06 15:45

题目: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

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

#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;}
原创粉丝点击