首页 > 其他 > 详细

递归函数

时间:2019-06-26 09:16:57      阅读:161      评论:0      收藏:0      [点我收藏+]

递归函数

    在一定程度上可以为循环

    自己调用自己本身的方法被称为递归函数

    *** 最重要的就是

 

6!

技术分享图片

 

public class Test {
    public static void main(String[] args) {
        //方法一(for循环)
        int result=1;
        
        for (int i = 6; i >= 1; i--) {
            result=result*i;
        }
        System.out.println(result);
        
        
        //方法二(递归)
        System.out.println(fac(6));    
    }
    public static int fac(int num) {
        if (num==1) {
            return 1;
        }
        return num*fac(num-1);
    }
    
}

 

 

 斐波那契数列

 

          技术分享图片

public class Demo3 {  
    // 使用递归方法  
    private static int getFibo(int i) {  
        if (i == 1 || i == 2)  
            return 1;  
        else  
            return getFibo(i - 1) + getFibo(i - 2);  
    }  
  
    public static void main(String[] args) {  
        System.out.println("斐波那契数列的前20项为:");  
        for (int j = 1; j <= 20; j++) {  
            System.out.print(getFibo(j) + "\t");  
            if (j % 5 == 0)  
                System.out.println();  
        }  
    }  
}  

 

递归函数

原文:https://www.cnblogs.com/YangYouHan/p/10949967.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!