2013
11-09

# Integer Inquiry

One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.

“This supercomputer is great,” remarked Chip. “I only wish Timothy were here to see these results.” (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Your program should output the sum of the VeryLongIntegers given in the input.

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

370370367037037036703703703670

import java.util.*;
import java.math.*;

public class Main {

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);

BigDecimal bd1 = BigDecimal.valueOf(0);
BigDecimal bd2 = BigDecimal.valueOf(0);
String str;

while(cin.hasNext())
{
str = cin.nextLine();
if(str.equals("0"))
break;
else
{
bd2 = new BigDecimal(str);
bd1 = bd1.add(bd2);
}
}

System.out.println(bd1.toPlainString());

}

}

1. 第二种想法，我想来好久，为啥需要一个newhead，发现是把最后一个节点一直返回到嘴上面这层函数。厉害，这道题之前没样子想过。

2. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮