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:\day06\day07
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("\t");
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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM