HDU1047Integer Inquiry
大数累加和问题:

题目的意思是:输入n组测试样例,每组测试样例个数不确定(以0结束),输出测试样例的累加和。
注意:

  • 使用BigInteger ans = BigInteger.ZERO;可以更方便的进行比较。
  • a.compareTo(b)方法:用作比较大小,a>b(返回1),a==b(返回0),a<b(返回-1)。
import java.util.Scanner;
import java.math.BigInteger;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		while (T != 0) {
			BigInteger ans = BigInteger.ZERO;//这样初始化更方便;
			BigInteger num = in.nextBigInteger();// 输入每组第一个数
			while (num.compareTo(BigInteger.ZERO) != 0) {
				ans = ans.add(num);//累加到ans
				num = in.nextBigInteger();//继续输入
			}
			System.out.println(ans);
			ans = BigInteger.ZERO;//初始化ans
			if (T != 1)
				System.out.println("");
			T--;
		}
	}
}