码迷,mamicode.com
首页 > 其他好文 > 详细

解决递归问题:递归方法和非递归方法

时间:2015-11-17 19:01:18      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

代码如下

package DiGuistyle;

public class digui {

    // 非递归方法
    // 设置static静态方法 可以用digui.notDiGui()类的形态调用方法
    static long notDiGui(int n){
        long result=1;
        for(int i=1;i<=n;i++){
            result=result*i;
        }
        return result;
    }
    
    /*
     * 递归编写方法如下
     */
    static long DiGui(int n){
        if(n==1){
            return 1;
        }
        
        return DiGui(n-1)*n;
    }
    
    
    public static void main(String[] args) {
        System.out.println("非递归方法:"+digui.notDiGui(5));
        System.out.println("递归方法:"+DiGui(5));
    }
}

结果为

非递归方法:120
递归方法:120

解决递归问题:递归方法和非递归方法

标签:

原文地址:http://www.cnblogs.com/xiaolongecom/p/4972384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!