java-递归练习

1、从键盘接收一个文件夹路径,统计该文件夹大小

 1 public class Test1 {
 2 
 3     /**
 4      * @param args
 5      * 需求:1,从键盘接收一个文件夹路径,统计该文件夹大小
 6      * 
 7      * 从键盘接收一个文件夹路径
 8      * 1,创建键盘录入对象
 9      * 2,定义一个无限循环
10      * 3,将键盘录入的结果存储并封装成File对象
11      * 4,对File对象判断
12      * 5,将文件夹路径对象返回
13      * 
14      * 统计该文件夹大小 
15      * 1,定义一个求和变量
16      * 2,获取该文件夹下所有的文件和文件夹listFiles();
17      * 3,遍历数组
18      * 4,判断是文件就计算大小并累加
19      * 5,判断是文件夹,递归调用
20      */
21     public static void main(String[] args) {
22         //File dir = new File("F:\day06");
23         //System.out.println(dir.length());                //直接获取文件夹的结果是0
24         File dir = getDir();
25         System.out.println(getFileLength(dir));
26         
27     }
28     
29     /*
30      * 从键盘接收一个文件夹路径
31      * 1,返回值类型File
32      * 2,参数列表无
33      */
34     public static File getDir() {
35         //1,创建键盘录入对象
36         Scanner sc = new Scanner(System.in);
37         System.out.println("请输入一个文件夹路径:");
38         //2,定义一个无限循环
39         while(true) {
40             //3,将键盘录入的结果存储并封装成File对象
41             String line = sc.nextLine();
42             File dir = new File(line);
43             //4,对File对象判断
44             if(!dir.exists()) {
45                 System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径:");
46             }else if(dir.isFile()) {
47                 System.out.println("您录入的是文件路径,请输入一个文件夹路径:");
48             }else {
49                 //5,将文件夹路径对象返回
50                 return dir;
51             }
52         }
53         
54     }
55     
56     /*
57      * 统计该文件夹大小 
58      * 1,返回值类型long
59      * 2,参数列表File dir
60      */
61     public static long getFileLength(File dir) {    //dir = F:day06day07
62         //1,定义一个求和变量
63         long len = 0;
64         //2,获取该文件夹下所有的文件和文件夹listFiles();
65         File[] subFiles = dir.listFiles();            //day07 Demo1_Student.class Demo1_Student.java
66         //3,遍历数组
67         for (File subFile : subFiles) {
68             //4,判断是文件就计算大小并累加
69             if(subFile.isFile()) {
70                 len = len + subFile.length();
71             //5,判断是文件夹,递归调用
72             }else {
73                 len = len + getFileLength(subFile);
74             }
75         }
76         return len;
77     }
78 }

2、从键盘接收一个文件夹路径,删除该文件夹

 1 public class Test2 {
 2 
 3     /**
 4      * 需求:2,从键盘接收一个文件夹路径,删除该文件夹
 5      * 
 6      * 删除该文件夹
 7      * 分析:
 8      * 1,获取该文件夹下的所有的文件和文件夹
 9      * 2,遍历数组
10      * 3,判断是文件直接删除
11      * 4,如果是文件夹,递归调用
12      * 5,循环结束后,把空文件夹删掉
13      */
14     public static void main(String[] args) {
15         File dir = Test1.getDir();                    //获取文件夹路径
16         deleteFile(dir);
17     }
18 
19     /*
20      * 删除该文件夹
21      * 1,返回值类型 void
22      * 2,参数列表File dir
23      */
24     public static void deleteFile(File dir) {    
25         //1,获取该文件夹下的所有的文件和文件夹
26         File[] subFiles = dir.listFiles();        
27         //2,遍历数组
28         for (File subFile : subFiles) {
29             //3,判断是文件直接删除
30             if(subFile.isFile()) {
31                 subFile.delete();
32             //4,如果是文件夹,递归调用
33             }else {
34                 deleteFile(subFile);            
35             }
36         }
37         //5,循环结束后,把空文件夹删掉
38         dir.delete();
39     }
40 }

3、从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

 1 public class Test3 {
 2 
 3     /**
 4      * 需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
 5      * 
 6      * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
 7      * 分析:
 8      * 1,在目标文件夹中创建原文件夹
 9      * 2,获取原文件夹中所有的文件和文件夹,存储在File数组中
10      * 3,遍历数组
11      * 4,如果是文件就用io流读写
12      * 5,如果是文件夹就递归调用
13      * @throws IOException 
14      */
15     public static void main(String[] args) throws IOException {
16         File src = Test1.getDir();
17         File dest = Test1.getDir();
18         if(src.equals(dest)) {
19             System.out.println("目标文件夹是源文件夹的子文件夹");
20         }else {
21             copy(src,dest);
22         }
23     }
24     /*
25      * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
26      * 1,返回值类型void
27      * 2,参数列表File src,File dest
28      */
29     public static void copy(File src, File dest) throws IOException {
30         //1,在目标文件夹中创建原文件夹
31         File newDir = new File(dest, src.getName());
32         newDir.mkdir();
33         //2,获取原文件夹中所有的文件和文件夹,存储在File数组中
34         File[] subFiles = src.listFiles();
35         //3,遍历数组
36         for (File subFile : subFiles) {
37             //4,如果是文件就用io流读写
38             if(subFile.isFile()) {
39                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
40                 BufferedOutputStream bos = 
41                         new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));
42                 
43                 int b;
44                 while((b = bis.read()) != -1) {
45                     bos.write(b);
46                 }
47                 
48                 bis.close();
49                 bos.close();
50             //5,如果是文件夹就递归调用
51             }else {
52                 copy(subFile,newDir);
53             }
54         }
55     }
56 }

4、从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印

 1 public class Test4 {
 2 
 3     /**
 4      * 需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如:
 5      * 把文件夹中的所有文件以及文件夹的名字按层级打印
 6      * 分析:
 7      * 1,获取所有文件和文件夹,返回的File数组
 8      * 2,遍历数组
 9      * 3,无论是文件还是文件夹,都需要直接打印
10      * 4,如果是文件夹,递归调用
11      *     day07
12      *         day08
13      *             xxx.jpg
14      *             yyy.txt
15      *         Demo1_Consturctor.class
16      *         Demo1_Consturctor.java
17      *     Demo1_Student.class
18      *     Demo1_Student.java
19      */
20     public static void main(String[] args) {
21         File dir = Test1.getDir();                //获取文件夹路径
22         printLev(dir,0);
23     }
24 
25     public static void printLev(File dir,int lev) {
26         //1,把文件夹中的所有文件以及文件夹的名字按层级打印
27         File[] subFiles = dir.listFiles();
28         //2,遍历数组
29         for (File subFile : subFiles) {
30             for(int i = 0; i <= lev; i++) {
31                 System.out.print("	");
32             }
33             //3,无论是文件还是文件夹,都需要直接打印
34             System.out.println(subFile);
35             //4,如果是文件夹,递归调用
36             if(subFile.isDirectory()) {
37                 //printLev(subFile,lev + 1);
38                 printLev(subFile,++lev);
39             }
40         }
41     }
42 
43 }

5、斐波那契数列

 1 public class Test5 {
 2 
 3     /**
 4      * * 不死神兔
 5     * 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
 6     * 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡,
 7     * 问:一对刚出生的兔子,一年内繁殖成多少对兔子?
 8     * 1 1 2 3 5 8 13 21
 9     * 1 = fun(1)
10     * 1 = fun(2)
11     * 2 = fun(1) + fun(2)
12     * 3 = fun(2) + fun(3)
13      */
14     public static void main(String[] args) {
15         //demo1();
16         System.out.println(fun(8));
17     }
18 
19     public static void demo1() {
20         //用数组做不死神兔
21         int[] arr = new int[8];
22         //数组中第一个元素和第二个元素都为1
23         arr[0] = 1;
24         arr[1] = 1;
25         //遍历数组对其他元素赋值
26         for(int i = 2; i < arr.length; i++) {
27             arr[i] = arr[i - 2] + arr[i - 1];
28         }
29         //如何获取最后一个数
30         System.out.println(arr[arr.length - 1]);
31     }
32 
33     /*
34      * 用递归求斐波那契数列
35      */
36     public static int fun(int num) {
37         if(num == 1 || num == 2) {
38             return 1;
39         }else {
40             return fun(num - 2) + fun(num - 1);
41         }
42     }
43 }

6、求出1000的阶乘所有零和尾部零的个数,不用递归做

 1 public class Test6 {
 2 
 3     /**
 4      * @param args
 5      *  需求:求出1000的阶乘所有零和尾部零的个数,不用递归做
 6      */
 7     public static void main(String[] args) {
 8         /*int result = 1;
 9         for(int i = 1; i <= 1000; i++) {
10             result = result * i;
11         }
12         
13         System.out.println(result);        //因为1000的阶乘远远超出了int的取值范围
14         */
15         //demo1();
16         demo2();
17     }
18 
19     public static void demo2() {        //获取1000的阶乘尾部有多少个零
20         BigInteger bi1 = new BigInteger("1");
21         for(int i = 1; i <= 1000; i++) {
22             BigInteger bi2 = new BigInteger(i+"");
23             bi1 = bi1.multiply(bi2);    //将bi1与bi2相乘的结果赋值给bi1
24         }
25         String str = bi1.toString();    //获取字符串表现形式
26         StringBuilder sb = new StringBuilder(str);
27         str = sb.reverse().toString();    //链式编程
28         
29         int count = 0;                    //定义计数器
30         for(int i = 0; i < str.length(); i++) {
31             if('0' != str.charAt(i)) {
32                 break;
33             }else {
34                 count++;
35             }
36         }
37         
38         System.out.println(count);
39     }
40 
41     public static void demo1() {        //求1000的阶乘中所有的零
42         BigInteger bi1 = new BigInteger("1");
43         for(int i = 1; i <= 1000; i++) {
44             BigInteger bi2 = new BigInteger(i+"");
45             bi1 = bi1.multiply(bi2);    //将bi1与bi2相乘的结果赋值给bi1
46         }
47         String str = bi1.toString();    //获取字符串表现形式
48         int count = 0;
49         for(int i = 0; i < str.length(); i++) {
50             if('0' == str.charAt(i)) {    //如果字符串中出现了0字符
51                 count++;                //计数器加1
52             }
53         }
54         System.out.println(count);
55     }
56 
57 }

7、求出1000的阶乘尾部零的个数,用递归做

 1 public class Test7 {
 2 
 3     /**
 4      * @param args
 5      * 需求:求出1000的阶乘尾部零的个数,用递归做
 6      * 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100...1000  1000 / 5 = 200
 7      * 5 * 5    5 * 5 * 2     5 * 5 * 3    5 * 5 * 4    5 * 5 * 5    5 * 5 * 6    200 / 5 = 40
 8      * 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
 9                                                                             40 / 5 = 8
10         5 * 5 * 5 * 5                                                        8 / 5 = 1
11      */
12     public static void main(String[] args) {
13         System.out.println(fun(1000));
14     }
15 
16     public static int fun(int num) {
17         if(num > 0 && num < 5) {
18             return 0;
19         }else {
20             return num / 5 + fun(num / 5);
21         }
22     }
23 }

8、约瑟夫环,幸运数字

 1 public class Test8 {
 2 
 3     /**
 4      * @param args
 5      * 约瑟夫环
 6      * * 幸运数字
 7      */
 8     public static void main(String[] args) {
 9         System.out.println(getLucklyNum(8));
10     }
11 
12     /*
13      * 获取幸运数字
14      * 1,返回值类型int
15      * 2,参数列表int num
16      */
17     public static int getLucklyNum(int num) {
18         ArrayList<Integer> list = new ArrayList<>();        //创建集合存储1到num的对象
19         for(int i = 1; i <= num; i++) {
20             list.add(i);                                    //将1到num存储在集合中
21         }
22         
23         int count = 1;                                        //用来数数的,只要是3的倍数就杀人
24         for(int i = 0; list.size() != 1; i++) {                //只要集合中人数超过1,就要不断的杀
25             if(i == list.size()) {                            //如果i增长到集合最大的索引+1时
26                 i = 0;                                        //重新归零
27             }
28             
29             if(count % 3 == 0) {                            //如果是3的倍数
30                 list.remove(i--);                                //就杀人
31             }
32             count++;
33         }
34         
35         return list.get(0);
36     }
37 }
原文地址:https://www.cnblogs.com/hfumin/p/10429799.html