题目描述
最近谷学长遇到了麻烦的问题,你能帮帮他吗?
题目是这样的:计算SUM(n) = 1 + 2 + 3 + ... + 10^n。满足n<=100 000。
输入描述
输入包含多组数据,每组数据一行,包括一个整数n(n<=100 000)。当n=-1时输入终止。
输出描述
对于每个n输出相应的和。
样例输入
1
2
-1
样例输出
55
5050
提示
人家……人家真的不是求和题啦 喵~
主要考虑数据类型容量问题。
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
int n = scanner.nextInt();
if (n == -1) {
break;
}
BigInteger maxsum = BigInteger.TEN.pow(n);
BigInteger sum = maxsum.multiply(maxsum.add(BigInteger.ONE)).divide(BigInteger.valueOf(2));
System.out.println(sum);
}
scanner.close();
}
}