###23.01_File类递归练习(统计该文件夹大小)
- 需求:1,从键盘接收一个文件夹路径,统计该文件夹大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package com.diedline.test;
import java.io.File; import java.util.Scanner;
public class Test1 {
public static void main(String[] args) { File c = isDir(); System.out.println(getSize(c));
} public static File isDir(){ Scanner sc = new Scanner(System.in); while (true){ String line = sc.nextLine(); File file = new File(line); if(!file.exists()){ System.out.println("错误的路径"); } else if (file.isFile()){ System.out.println("输入的是一个文件路径请重新输入"); } else { return file; } } }
public static long getSize(File a){ File[] arr = a.listFiles(); long size = 0; for (File b:arr ) { if (b.isFile()){ size += b.length(); } else { getSize(b); } } return size; } }
|
###23.02_File类递归练习(删除该文件夹)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.diedline.test;
import java.io.File;
public class Test2 {
public static void main(String[] args) { File dir = Test1.isDir(); delateFile(dir);
}
public static void delateFile(File dir){ File[] arr = dir.listFiles(); for (File a:arr ) { if (a.isFile()){ a.delete(); } else { delateFile(a); } } dir.delete(); System.out.println("已经删除完了"); } }
|
###23.03_File类递归练习(拷贝)
- 需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.diedline.test;
import java.io.*;
public class Test3 {
public static void main(String[] args) throws IOException { System.out.println("输入两个文件夹"); File a = Test1.isDir(); File b = Test1.isDir(); if(a.equals(b)){ System.out.println("目标文件夹是原文件夹的子文件夹"); }else { fileCopy(a,b); } }
public static void fileCopy(File a,File b) throws IOException { File newDir = new File(b,a.getName()); newDir.mkdir(); File[] subFiles = a.listFiles(); for (File subFile:subFiles ) { if(subFile.isFile()){ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName()))); int c; while ((c = bis.read()) != -1){ bos.write(c); } bis.close(); bos.close(); } else { fileCopy(subFile,newDir); } }
} }
|
###23.04_File类递归练习(按层级打印)
需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如:
aaa是文件夹,里面有bbb.txt,ccc.txt,ddd.txt这些文件,有eee这样的文件夹,eee中有fff.txt和ggg.txt,打印出层级来
aaa
bbb.txt
ccc.txt
ddd.txt
eee
fff.txt
ggg.txt
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.diedline.test;
import java.io.File;
public class Test4 {
public static void main(String[] args) { File dir = Test1.isDir(); printLev(dir,0); } public static void printLev(File dir,int lev){ File[] arr = dir.listFiles(); for (File a:arr ) { for (int i = 0; i <= lev; i++){ System.out.print("\t"); } System.out.println(a.getName()); if (a.isDirectory()){ printLev(a,lev + 1); } } } }
|
###23.05_递归练习(斐波那契数列)
- 不死神兔
- 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
- 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡,
- 问:一对刚出生的兔子,一年内繁殖成多少对兔子?
- 1 1 2 3 5 8 13
- 第一个月一对小兔子 1
- 第二个月一对大兔子 1
- 第三个月一对大兔子生了一对小兔子 2
- 第四个月一对大兔子生了一对小兔子
- 一对小兔子长成大兔子 3
- 第五个月两对大兔子生两对小兔子
- 一对小兔子长成大兔子 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.diedline.test;
public class Test5 {
public static void main(String[] args) {
System.out.println(fun(8)); }
public static void demo1() { int[] arr = new int[8]; arr[0] = 1; arr[1] = 1; for (int i = 0; i < arr.length - 2; i++) { arr[i + 2] = arr[i] + arr[i + 1]; } for (int a : arr ) { System.out.print(a + " "); } }
public static int fun(int num) { if (num == 1 || num == 2) { return 1; } else { return fun(num - 1) + fun(num - 2); } } }
|
###23.06_递归练习(1000的阶乘所有零和尾部零的个数)
- 需求:求出1000的阶乘所有零和尾部零的个数,不用递归做
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.diedline.test;
import java.math.BigInteger;
public class Test6 {
public static void main(String[] args) { String a = factorial(1000).toString(); System.out.println("所有的0的个数:"+getZero(a)); getLastZero(a); }
public static void getLastZero(String a) { StringBuilder sb = new StringBuilder(a); a = sb.reverse().toString(); int count = 0; for(int i = 0;i<a.length();i++){ if('0' != a.charAt(i)){ break; } else count++; } System.out.println("尾部0的个数:"+count); }
public static BigInteger factorial(int a){ BigInteger result = new BigInteger("1"); for (int i = 1; i < a+1; i++) { BigInteger result2 = new BigInteger(i+""); result = result.multiply(result2); } return result; }
public static int getZero(String a){ int count = 0; for (int i = 0; i <a.length() ; i++) { if('0' == a.charAt(i)){ count++; } } return count; } }
|
###23.07_递归练习(1000的阶乘尾部零的个数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.diedline.test;
public class Test7 {
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); } } }
|
###23.08_集合练习(约瑟夫环)
- 幸运数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.diedline.test;
import java.util.ArrayList;
public class Test8 {
public static void main(String[] args) { System.out.println(getLucklyNum(8)); }
public static int getLucklyNum(int num){ ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <num + 1 ; i++) { list.add(i); } int count = 1; for (int i = 0; list.size() != 1;i++){ if(i==list.size()){ i = 0; } if (count % 3 ==0){ list.remove(i--); } count++; } return list.get(0); } }
|