PKU 2000 "Gold Coins"

問題:http://acm.pku.edu.cn/JudgeOnline/problem?id=2000
順位:http://acm.pku.edu.cn/JudgeOnline/problemstatus?problem_id=2000&orderby=clen&language=2
提出コード(204B)

import java.util.*;class Main{static{Scanner s=new Scanner(System.in);for(int n,i;(n=s.nextInt())>0;){i=1+(int)(Math.sqrt(8*n-1)-1)/2;System.out.printf("%d %d\n",n,i*(i-1)*(2*i-1)/6+i*(n-i*(i-1)/2));}}}

整形コード

class Main {
	static {
		Scanner s = new Scanner(System.in);
		for (int n, i; (n = s.nextInt()) > 0;) {
			i = 1 + (int) (Math.sqrt(8 * n - 1) - 1) / 2;
			System.out.printf("%d %d\n", n, i * (i - 1) * (2 * i - 1) / 6 + i
					* (n - i * (i - 1) / 2));
		}
	}
}