提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
带for循环的累积和
public class Main { public static void main(String args[]) { int i, n = 10; /* *来 自 时 代 J a v a - N o w J a v a . c o m */ int cumulLoop = 0; for (i = 0; i < n; i++) { cumulLoop += i; } int cumul = (n * (n - 1)) / 2; // closed-form solution System.out.println(cumulLoop + " closed-form:" + cumul); } }