zoukankan      html  css  js  c++  java
  • wikioi 3116 高精度练习之加法

    题目描述 Description

    给出两个正整数A和B,计算A+B的值。保证A和B的位数不超过500位。

    输入描述 Input Description

    读入两个用空格隔开的正整数

    输出描述 Output Description

    输出A+B的值

    样例输入 Sample Input

    3 12

    样例输出 Sample Output

    15

    数据范围及提示 Data Size & Hint

    两个正整数的位数不超过500位

    用string来存输入的整数,获取两个整数的位数,然后,用整形数组来存两个整数相加的结果。

    /*
    作者:t_rex
    题目:p3116 高精度练习之加法
    */
    
    #include <iostream>
    #include <string>
    using namespace std;
    void add2num(string a, string b){
        int len_a = a.size(), len_b = b.size();
        int len = max(len_a, len_b) + 1;
        int i = len_a-1, j = len_b-1, carry = 0, p = 0, sum = 0;
        int* s = new int[len + 1];
        while(i >= 0 || j >= 0){
            sum = 0;
            if(i >= 0) sum += a[i--] - '0';
            if(j >= 0) sum += b[j--] - '0';
            sum += carry;
            s[p++] = sum%10;
            carry = sum/10;
        }
        if(carry == 1)s[p++] = 1;
        for(i = p-1; i >= 0; i--) cout<<s[i];
        delete []s;
    }
    int main()
    {
        string a, b;
        cin >> a >> b;
        add2num(a, b);
        return 0;
    }
    















    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    函数式宏定义与普通函数
    linux之sort用法
    HDU 4390 Number Sequence 容斥原理
    HDU 4407 Sum 容斥原理
    HDU 4059 The Boss on Mars 容斥原理
    UVA12653 Buses
    UVA 12651 Triangles
    UVA 10892
    HDU 4292 Food
    HDU 4288 Coder
  • 原文地址:https://www.cnblogs.com/Rex7/p/4752552.html
Copyright © 2011-2022 走看看