当前位置: 代码迷 >> Java相关 >> 求高手用for循环帮帮小弟我(最好写上注释)
  详细解决方案

求高手用for循环帮帮小弟我(最好写上注释)

热度:2863   发布时间:2013-02-25 21:48:36.0
求高手用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;    }}
  相关解决方案