题目:•求1!+2!+3!+…+n!, n<=15。
示例代码:
package cn.wdl.demo; public class Case10 { public int fac(int n) { int fac=1; for(int i=1;i<=n;i++) { fac = fac*i; } return fac; } public int getFactorSum(int n) { int sum=0; for(int i=1;i<=n;i++) { sum = sum + fac(i); } return sum; } public static void main(String[] args) { Case10 c = new Case10(); int n=3; System.out.println("1!+2!+...+"+n+"!="+c.getFactorSum(n)); } }
运行结果:
1!+2!+...+5!=153