日期:2014-05-20  浏览次数:20631 次

求高手用for循环帮帮我(最好写上注释)
编写程序计算:
  1!+(1!+2!)+(1!+2!+3!)+(。。。。。。。。。。)+(1!+2!+3!+。。。。+5!)的值
  其中n!=n*(n-1)*(n-2)*..........*2*1

------解决方案--------------------
我写一个Java的实现代码,虽然不够精简,但是正确性、易读性应该没问题吧。
Java code

public class Temp{
    public static void main(String[] args) {
        System.out.println(zong(5));
    }
    public static int zong(int n){
        //计算若干个多项式的总和
        //如计算(1!) + (1! + 2!) + (1! + 2! + 3!)的数值
        //(1!) + (1! + 2!) + (1! + 2! + 3!) + (1! + 2! + 3! + 4!)的数值
        int result = 0;
        for(; n > 0; n--){
            result += duo(n);
        }
        return result;
    }
    public static int duo(int n){
        //计算每一个多项式(1! + 2! + ... + n!)的数值
        //如计算(1!)的数值,(1! + 2!)的数值
        //(1! + 2! + 3!)的数值
        int result = 0;
        for(; n > 0; n--){
            result += jie(n);
        }
        return result;
    }
    public static int jie(int n){
        //计算每一个单独的n!的数值(阶乘)
        //如计算1!的数值,2!的数值,3!的数值
        int result = 1;
        for(; n > 0; n--){
            result *= n;
        }
        return result;
    }
}