题意:
先输入一个非负正整数n,n为测试实例次数,每次输入多行大数,最后一行为0表示此次输入结束,计算0以前若干行的大数和,并且输出。共n行输出,第两行输出之间有一个空行。
示例输入:
1
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
示例输出:
370370367037037036703703703670
解决方案:
题目是全英文,看题目花了些许时间,才明白什么意思,中间有次Presentation Error(描述错误),因为题意有句The output format consists of N output blocks. There is a blank line between output blocks.就是两个输出之间才换行,最后一行不用换行。
BigInteger类中封装了add()函数,实现两数的相加,a.add(b)将返回a+b的值。
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
BigInteger sum, a;
int n;
n = input.nextInt();
for (int i = 0; i < n; i++)
{
sum = BigInteger.ZERO;//sum=0
a = input.nextBigInteger();
while (!a.equals(BigInteger.ZERO))
{
sum = sum.add(a);//sum=sum+a
a = input.nextBigInteger();
}
if (i == 0)
System.out.println(sum);
else
{
System.out.println("");
System.out.println(sum);
}
}
}
}