1159. Sum 高精度的计算 注意细心啊 很好的题呢 有考察函数调用

1159. Sum
 

Time Limit: 1sec    Memory Limit:32MB
Description

Given several positive integers, calculate the sum of them.

Input
There are several test cases, one line for each case. For each line, the first number, N (N<=100000), is the number of positive numbers and then N positive numbers followed. The sum will not exceed 10 100.
Output
Output each sum in a single line without leading zeros.
Sample Input
 Copy sample input to clipboard
3 1 2 31 1232 1111111111111111111111111111111111 1111111111111111111111111111111111
Sample Output
61232222222222222222222222222222222222

Problem Source: ZSUACM Team Member



代码有参考别人的 不记得是哪里来的了啦 高精度还是需要细心啊 但是不难理解


#include <iostream>
#include <string>
#include <memory.h>

using namespace std;

void input (int num[]) {  //从string变成int的数组
    string a;
    cin>>a;
    int size = a.length();
    for (int i = 0; i< size; i++) {
        num[i] = a.at(size - i -1) - '0';
    }
}

void output (int sum[]){
    int i;
    for (i = 199; sum[i] == 0; i--) {} //这里吧0的位都跳过了(倒序输出)
    for (;i >= 0; i--) { //从第一个非0位开始输出
        cout<<sum[i];
    }
}

void add (int num[], int sum[]) {
    for(int i = 0; i < 200; i++) {
        sum[i] += num[i];
        sum[i+1] += sum[i]/10;
        sum[i] = sum[i] % 10;
    }
}

int main () {
    int n;
    while (cin>>n && n) {
        int sum[200], num[200];
        memset(sum, 0, sizeof(sum)); //头文件#include <memory.h> 不要漏了
        memset(num, 0, sizeof(num));
        for (int i = 0; i < n; i++) {
            input(num);
            add(num, sum);
            memset(num, 0, sizeof(num));
        }
        output(sum);
        cout<<endl;
    }

    return 0;
}                                 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值