java-大数据求和计算

package com.cc.thread;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 给定一组自然数,数字的值有可能会大于2^64 ,要求计算出所有数字的和
 */
public class Test1 {
    public static void main(String[] args) {
        System.out.println(new Test1().sum(new ArrayList<>(Arrays.asList("20200201123456","20200201123456789","20200201123456789123456789"))));
    }

    public String sum (ArrayList<String> strings) {
        BigInteger bgSum = new BigInteger("0");
        for(String str : strings){
            bgSum = bgSum.add(new BigInteger(str));
        }
        return bgSum.toString();
    }
}

 

上一篇:java – Long vs BigInteger


下一篇:Game of Taking Stones HDU - 5973