本帖最后由 陈圳 于 2013-4-12 15:37 编辑
- /**计算n = n*(n-1)*(n-2)*(n-3)......
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(getNumltipli2(4,2));
- System.out.println(getMultipli1(4,2));
- }
- public static int getMultipli1(int num,int n){//递归
- if(n==0)
- return num;
- return num*=getMultipli1(num-1,n-1);
- }
- public static int getNumltipli2(int num,int n){//for循环
- int sum=1;//你的错误在这里!必须改为1,如果是0.怎么除结果肯定都是0;
- int length=n;
- for(int i=0;i<=length;i++){
- sum*=num--;
- }
- return sum;
- }
复制代码 |