zoukankan      html  css  js  c++  java
  • Java 递归

    递归

            1,方法定义中自身调用自身的现象。

           2,注意事项:

                    1,递归方法没有出口,会成为无限循环,直到溢出。//StackOverflowError

                    2,递归次数过多,会导致栈内存溢出。

                    3,构造方法不能定义递归。

           3,案例:

                   1,求20的阶乘

    /** 
    *    求阶乘 :20!
    *    出口:当阶乘成为1的时候结束
    *    规律:n(n-1)!
    *
    */
    public class Recursion{
        public static void main(String[] args){
            System.out.println(rec(20));
        }
    
        public static int rec(int n ){
            if(n==1){
                return 1;    
            }else{
                return n*rec(n-1);
            }
        }
    }

                   2,古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问20个月的兔子对数为多少?

    /**
    *      分析:
    *              1,1,2,3,5,8,13.....
    *      出口:第1,2,3月已知
    *      规律:从第三项开始,第一项是前两项之和
    */
    public class Rabbit{
        public static void main(String[] args){
            System.out.println(getRabbit(20));
        }
        public static int getRabbit(int n){
            if(n==1||n==2){
                return 1;
            }else{
               return getRabbit(n-1)+getRabbit(n-2);
            }
        }
    }

                   3,输出d:\盘以.java结尾文件路径

    /**
    *       分析:
    *             1   创建目录,获取该目录的File数组         
    *             2   遍历File数组,获取每一个File对象
    *             3   判断该File是否是文件夹
    *                            是   回到第2步
    *                            否   是否以.java结尾
    *                                         是  输出该文件的绝对路径
    *                                         否  
    */
    public SearchDemo{
        public static void main(String[] args){
            File srcFile=new File("d:\");//d盘下目录过多,会报NullPointerExcetpion 
            getPath(srcFile);
        }    
        public static void getPath(File srcFile){
             File[] fileArray=srcFile.listFiles();
             for(File file: fileArray){
                 if(file.isDirectory()){
                     getPath(file); 
                 }else{
                     if(file.getName().endsWith(".java")){
                         System.out.println(file.getAbsolutePath());
                     }   
                 }     
             }
        }
    }

                   4,删除d:\盘以.java结尾的文件

    /**
    *      分析:
    *             1 封装目录,获取该目录的file数组
    *             2 获取每一个file数组
    *             3 判断是否为目录
    *                      是  返回第2步
    *                      否   是否以.java结尾
    *                                   是  删除
    *                                   否
    */
    public class DeleteDemo{
        public static void main(String[] args){
            File srcFile=new File("d:\"); //d盘下目录过多,会报NullPointerExcetpion
            deleteFile(srcFile);
        }
        public static void deleteFile(File srcFile){
            File[] fileArray=srcFile.listFiles();
            if(fileArray!=null){
                 for(File file: fileArray){
                    if(file.isDirectory()){
                        deleteFile(file);
                     }else{
                         if(file.getName().endsWith(".java")){
                            System.out.println(file.getName()+"-"file.delete());
                         }
                     }
                 System.out.println(srcFile.getName()+""+srcFile.delete());  //删除已经清空的文件夹         
                 }
            }
        }
    }

  • 相关阅读:
    Educational Codeforces Round 88 (Rated for Div. 2) D. Yet Another Yet Another Task(枚举/最大连续子序列)
    Educational Codeforces Round 88 (Rated for Div. 2) A. Berland Poker(数学)
    Educational Codeforces Round 88 (Rated for Div. 2) E. Modular Stability(数论)
    Educational Codeforces Round 88 (Rated for Div. 2) C. Mixing Water(数学/二分)
    Codeforces Round #644 (Div. 3)
    Educational Codeforces Round 76 (Rated for Div. 2)
    Educational Codeforces Round 77 (Rated for Div. 2)
    Educational Codeforces Round 87 (Rated for Div. 2)
    AtCoder Beginner Contest 168
    Codeforces Round #643 (Div. 2)
  • 原文地址:https://www.cnblogs.com/zhanfuxing/p/3648420.html
Copyright © 2011-2022 走看看