UVA10013 Super long sums【大数】

The creators of a new programming language D++ have found out that whatever limit for SuperLongInttype they make, sometimes programmers need to operate even larger numbers. A limit of 1000 digitsis so small... You have to find the sum of two numbers with maximal size of 1.000.000 digits.

Input

The first line of a input file is an integer N, then a blank line followed by N input blocks. The first lineof an each input block contains a single number M (1 ≤ M ≤ 1000000) the length of the integers (inorder to make their lengths equal, some leading zeroes can be added). It is followed by these integerswritten in columns. That is, the next M lines contain two digits each, divided by a space. Each of thetwo given integers is not less than 1, and the length of their sum does not exceed M.

There is a blank line between input blocks.

Output

Each output block should contain exactly M digits in a single line representing the sum of these two integers.

  There is a blank line between output blocks.

Sample Input

2


4

0 4

4 2

6 8

3 7


3

3 0

7 9

2 8

Sample Output

4750


470


问题链接UVA10013 Super long sums

问题简述:(略)

问题分析:大数计算问题,模拟计算即可。

程序说明

  按位加法计算中,每一位的相是三个数相加,包括进位位,同时得到两个结果,一是本位数字,二是进位。

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA10013 Super long sums */

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6;
int a[N], b[N];

int main()
{
    int  n, m, t;
    int digit, carry;

    cin >> n;
    while(n--) {
        string ans = "";

        cin >> m;

        t = m;
        while(t--)
            cin >> a[t] >> b[t];

        carry = 0;
        for(int i=0; i<m; i++) {
            digit = a[i] + b[i] + carry;
            carry = digit / 10;
            digit %= 10;
            ans += digit + '0';
        }

        reverse(ans.begin(), ans.end());
        cout << ans << endl;

        if(n)
            cout << endl;
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值