HDU-1001-(Sum Problem)

作者: itbird01 | 来源:发表于2021-11-15 14:14 被阅读0次

HDU-1001-(Sum Problem)

解题思路

1.通过累加或者数学公式n(n+1)/2
2.n
(n+1)可能超过32-bit signed integer
3.题目要求输出空行

解题遇到的问题

.n*(n+1)可能超过32-bit signed integer

后续需要总结学习的知识点

##解法1
public class Main {

    public static void main(String[] args) {
        Scanner mScanner = new Scanner(System.in);
        while (mScanner.hasNextBigInteger()) {
            BigInteger a = mScanner.nextBigInteger();
            BigInteger b = a.add(new BigInteger("1"));
            BigInteger ans = a.multiply(b).divide(new BigInteger("2"));
            System.out.println(ans.toString());
            System.out.println();
        }
        mScanner.close();
    }

}

相关文章

网友评论

    本文标题:HDU-1001-(Sum Problem)

    本文链接:https://www.haomeiwen.com/subject/vswwzltx.html