Day23Java基础学习(递归的综合例题)

Day23

  1. File类递归练习(统计该文件夹大小)
    需求:1,从键盘接收一个文件夹路径,统计该文件夹大小
	public class Test1 {
    /*
    从键盘接收一个文件夹路径,统计该文件夹大小从键盘接收一个文件夹路径,统计该文件夹大小
    1:创建键盘录入对象
    2:定义一个无限循环
    3:将键盘录入的结果存储,并封装成File对象
    4:对File对象判断
    5:对文件夹路径对象返回

    统计该文件夹大小:
    1:定义一个求和变量
    2:获取该文件夹下所有的文件和文件夹listFiles()
    3:遍历数组
    4:判断是文件就计算大小并累加
    5:判断是文件夹,递归调用
     */
    public static void main(String[] args) {
      //File dir = new File("D:\\javastudy\\java");
      //  System.out.println(dir.length()); //直接获取文件夹大小为0
       File dir = getDir();
       System.out.println(getFileLength(dir));

    }
    /*
       从键盘接收一个文件夹路径:
       1.返回值类型File
       2:参数列表无

     */
    public static File getDir(){
      //1:创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个文件夹路径:");
        // 2:定义一个无限循环
        while (true) {
            String line = sc.nextLine();
            File dir = new File(line);
            //4:对File对象判断
            if(!dir.exists()) {
                System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径");
            }else if(dir.isFile()){
                System.out.println("您录入的是文件路径,请输入文件夹路径");
            }else {
                //5:判断是文件夹,递归调用
                return dir;
            }
        }
    }
    /*
    统计该文件夹大小:
    1:返回值类型long
    2:参数列表File dir
     */
    public static long getFileLength(File dir){
        // 1:定义一个求和变量
        long len = 0;
        //2:获取该文件夹下所有的文件和文件夹listFiles()
        File[] subFiles = dir.listFiles();
        //3:遍历数组
        for(File subFile : subFiles){
           // 4:判断是文件就计算大小并累加
            if(subFile.isFile()) {
                len = len + subFile.length();
            // 5:判断是文件夹,递归调用
            }else{
                len = len + getFileLength(subFile);
            }
        }
        return len;
    }
}

  1. File类递归练习(删除该文件夹)
    需求:2,从键盘接收一个文件夹路径,删除该文件夹。
public class Test2 {
    /*
    从键盘接收一个文件夹路径,删除该文件夹。
    1:获取该文件夹下的所有文件和文件夹
    2:遍历数组
    3:判断是文件直接删除
    4:如果是文件夹,递归调用
    5:循环结束后,把空文件夹删除
     */
    public static void main(String[] args) {
        File dir = Test1.getDir();  //获取文件夹路径
        deleteFile(dir);
    }
    /*
    删除该文件夹:
    1:返回值类型void
    2:参数列表File dir

     */
    public static void deleteFile(File dir){
        // 1:获取该文件夹下的所有文件和文件夹
        File[] subFiles = dir.listFiles();
        //2:遍历数组
        for(File subFile : subFiles){
            if (subFile.isFile()){
                subFile.delete();
                // 4:如果是文件夹,递归调用
            }else {
                deleteFile(subFile);
            }
        }
        //5:循环结束后,把空文件夹删除
        dir.delete();
    }
}

  1. File类递归练习(拷贝)
    需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中。
public class Test3 {
    /*
    需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中。

    把其中一个文件夹中(包含内容)拷贝到另一个文件夹中。
    1:在目标文件夹中创建原文件夹
    2:获取源文件夹中所有的文件和文件夹,存储在File数组中
    3:遍历数组
    4:如果是文件就用io流读写
    5:如果是文件夹就直接递归调用
     */
    public static void main(String[] args) throws IOException {
        File src = Test1.getDir();
        File dest = Test1.getDir();
        if(src.equals(dest)){
            System.out.println("目标文件夹是源文件夹的子文件夹");
        }else {
            copy(src, dest);
        }

    }
    /*
          把其中一个文件夹中(包含内容)拷贝到另一个文件夹中。
          1:返回值类型void
          2;参数列表 File src ,File dest
         */
    private static void copy(File src, File dest) throws IOException {
        //1:在目标文件夹中创建原文件夹
        File newDir = new File(dest,src.getName());
        newDir.mkdir();
        // 2:获取源文件夹中所有的文件和文件夹,存储在File数组中
        File[] subFiles = src.listFiles();
        for(File subFile : subFiles) {
            // 4:如果是文件就用io流读写
            if(subFile.isFile()){
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));

                int b;
                while ((b = bis.read()) != -1){
                    bos.write(b);
                }
                bis.close();
                bos.close();
            // 5:如果是文件夹就直接递归调用
            }else {
                copy(subFile,newDir);
            }
        }

    }

}


  1. File类递归练习(按层级打印)
    需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印。
public class Test4 {
    /*
    需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印。
    1:获取所有的文件和文件夹,返回file数组
    2:遍历数组
    3:无论是文件还是文件夹,都需要直接打印
    4:如果是文件夹,递归调用
     */
    public static void main(String[] args) {
        File dir = Test1.getDir();
        printLev(dir,0);

    }

    private static void printLev(File dir,int lev) {
        //把文件夹中的所有文件以及文件夹的名字按层级打印
        File[] subFiles = dir.listFiles();
        //2:遍历数组
        for(File subFile : subFiles){
            for(int i = 0; i <= lev; i++){
                System.out.print("\t");
            }
            //3:无论是文件还是文件夹,都需要直接打印
            System.out.println(subFile);
            // 4:如果是文件夹,递归调用
            if(subFile.isDirectory()){
                printLev(subFile,lev+1);
            }
        }

    }
}

  1. 递归练习(裴波那契约数列)
    不死神兔
    假设一对刚出生的小兔子一个月之后能长成大兔子,再过一个月就能生下一对小兔,一年内没有发生死亡。问:一对刚出生的兔子,一年内繁殖成多少只兔子。
    1 1 2 3 5 8 13
public class Test5 {
    /*
    假设一对刚出生的小兔子一个月之后能长成大兔子,
    再过一个月就能生下一对小兔,一年内没有发生死亡。
    问:一对刚出生的兔子,一年内繁殖成多少只兔子。
   1 1 2 3 5 8 13
   第一个月:1对小兔子                           1
   第二个月:1对大兔子                           1
   第三个月:1对大兔子生了一对小兔子              2
   第四个月:1对大兔子生了一对小兔子,
             一对小兔子长成了一对大兔子           3
    第五个月:两对大兔子生了两对小兔子,
             一对小兔子长成了一对大兔子           5

    规律:第三个数等于前两个的和
    1 = fun(1)
    1 = fun(2)
    2 = fun(1) + fun(2)
    3 = fun(2) + fun(3)

     */
    public static void main(String[] args) {
        //递归的方法
        System.out.println(fun(8));

    }

    private static void demo1() {
        //用数组做不死神兔
        int[] arr = new int[8];
        //数组中第一个元素和第二个元素都为1
        arr[0] = 1;
        arr[1] = 1;
        //遍历数组,并赋值
        for (int i = 2; i < arr.length; i++) {
            arr[i] = arr[i - 2] + arr[i - 1];
        }
        //如何获取最后一个数
        System.out.println(arr[arr.length - 1]);
    }
    /*
    递归的方法
     */
    public static int fun(int num){
        if (num == 1 || num == 2){
            return 1;
        }else {
            return (fun(num - 2) + fun(num - 1));
        }

    }


}
  1. 递归练习(1000的阶乘所有零和尾部零的个数)
    需求:1000的阶乘所有零和尾部零的个数,不用递归做
public class Test6 {
    /*
     需求:1000的阶乘所有零和尾部零的个数,不用递归做
     */
    public static void main(String[] args) {
       /* int result = 1;
        for(int i = 1; i <=1000; i++){
            result = result * i;
        }
        System.out.println(result);
        */                       //1000的阶乘超出了int的范围
        //demo1();
        
        BigInteger bi1 = new BigInteger("1"); 求1000的阶乘尾部的0
        for(int i = 1; i <= 1000; i++){
            BigInteger bi2 = new BigInteger(i+"");
            bi1 = bi1.multiply(bi2);  //将bi1与bi2相乘的结果赋值给bi1
        }
        String str = bi1.toString(); //获取字符串表现形式
        StringBuilder sb = new StringBuilder(str);
        str = sb.reverse().toString();  //链式编程,将字符串反转
        int count = 0;//定义计数器
        for(int i = 0; i < str.length(); i++){
            if('0' != str.charAt(i)){
                break;
            }else {
                count++;
            }
        }
        System.out.println(count);

    }

    private static void demo1() {   //求1000的阶乘所有的0
        BigInteger bi1 = new BigInteger("1");
        for(int i = 1; i <= 1000; i++){
            BigInteger bi2 = new BigInteger(i+"");
            bi1 = bi1.multiply(bi2);  //将bi1与bi2相乘的结果赋值给bi1
        }
        String str = bi1.toString(); //获取字符串表现形式
        int count = 0;
        for(int i = 0; i < str.length(); i++) {
            if('0' == str.charAt(i)) {  //如果字符串中出现了0字符
                count++;     //计数器加1
            }
        }
        System.out.println(count);
    }
}
  1. 递归练习(1000的阶乘尾部零的个数)
    需求:1000的阶乘所有零和尾部零的个数,用递归做
public class Test7 {
    /*
    需求:1000的阶乘尾部零的个数,用递归做
    5 10 15 20 25 30 35  40 45 50 55 60 65 70 75 80 85 90 95 100...   1000 / 5 = 200
    25 = 5 * 5 ; 50 = 5 * 5 * 2 ; 75 = 5 * 5 * 3; 100 = 5 * 5 * 4;...  200 / 5 = 40
    5 * 5 * 5 * 1   5 * 5 * 5 * 2  5 * 5 * 5 * 3  5 * 5 * 5 * 4  5 * 5 * 5 * 5  5 * 5 * 5 * 6  5 * 5 * 5 * 7  5 * 5 * 5 * 8
                                                                          40 / 5 = 8
      5 * 5 * 5 * 5 * 1                                                    8 / 5 = 1
     */
    public static void main(String[] args) {
        System.out.println(fun(1000));

    }
    public static int fun(int num) {
        if(num > 0 && num < 5){
            return 0;
        }else {
            return num / 5 + fun(num / 5);
        }
    }
}

  1. 集合练习(约瑟夫环)
    幸运数字。
public class Test8 {
    /*
    约瑟夫环:幸运数字
     */
    public static void main(String[] args) {
        System.out.println(getLucklyNum(8));

    }

    /*
    获取幸运数字
    1.返回值类型int
    2:参数列表int num
     */
    public static int getLucklyNum(int num) {
        ArrayList<Integer> list = new ArrayList<>(); //创建集合存储1到num的对象
        for (int i = 1; i <= num; i++) {
            list.add(i);                        //将1到num存储在集合中
        }
        int count = 1;   //用来数数的,只要是三的倍数就杀人
        for (int i = 0; list.size() != 1; i++) { //只要集合额人数超过1就接着杀
            if (i == list.size()) {     //如果i增长到集合最大的索引+1时重新归0
                i = 0;
            }
            if (count % 3 == 0) {   //如果时3的倍数
                list.remove(i--);   //就杀人

            }
            count++;

        }
        return list.get(0);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值