File class recursive exercise
Receive a folder path from the keyboard and count the size of the folder
Solution:
- Create keyboard entry object
- Define infinite loop
- Store and encapsulate the result of keyboard entry into File object
- Judge the File object
- Return the folder path object
- Define a summation variable
- Get all files and folders under the file listFiles()
- Traversal array
- If it is judged to be a file, calculate and accumulate the size
- Determine whether it is a folder recursive call
import java.io.File; import java.util.Scanner; public class test1 { public static void main(String[] args) { File dir = getDir(); System.out.println(get(dir)); } public static File getDir() { Scanner sc = new Scanner(System.in); System.out.println("Please enter folder path:"); while(true) { String s = sc.nextLine(); File dir = new File(s); if(!dir.exists()){ System.out.println("The folder path you entered does not exist. Please re-enter a folder:"); } else if(dir.isFile()){ System.out.println("You have entered a file path. Please enter a folder path:"); }else { System.out.println("Entered successfully!!!"); return dir; } } } public static Long get(File dir) { long len = 0; File[] subFiles = dir.listFiles(); for(File subfile : subFiles){ if(subfile.isFile()){ len = len + subfile.length(); }else{ len = len + get(subfile); } } return len; } }
Delete this folder
1. Get all the files and folders under this folder
Traversal array 2
3. If it is judged as a folder, continue recursion
4. If it is a file, delete it directly
5. Delete the empty folder after the cycle ends
import java.io.File; import java.util.Scanner; public class test2 { public static void main(String[] args) { File dir = getdir(); deletedir(dir); } public static File getdir() { Scanner sc = new Scanner(System.in); System.out.println("Please enter a folder path:"); while(true) { String line = sc.nextLine(); File dir = new File(line); if(!dir.exists()) { System.out.println("The folder path you entered does not exist, please re-enter:"); } else if(dir.isFile()){ System.out.println("You have entered a file path, please re-enter:"); } else { System.out.println("End of entry"); return dir; } } } public static void deletedir(File dir) { File[] subFiles = dir.listFiles(); for(File f : subFiles) { if(f.isDirectory()){ deletedir(f); }else { f.delete(); } } dir.delete(); } }
Copy
Problem solving ideas
Receive two folder paths from the keyboard and copy the contents of one folder to the other
1. Enter the path with the keyboard and create the original folder in the target folder
2. Obtain all files and folders in the original File folder and store them in the File array
3. Traverse the array
4. If it is a file, use io stream to read and write
5. If it is a folder, it will be called recursively
import java.io.*; public class test3 { public static void main(String[] args) throws IOException { File src = test1.getDir(); File dest = test1.getDir(); if(src.equals(dest)) { System.out.println("The destination folder is a subfolder of the source folder"); } else { copy(src, dest); } } public static void copy(File src,File dest) throws IOException { //Create the original folder in the destination folder File newDir = new File(dest,src.getName()); newDir.mkdir(); //Get all files and folders in the source folder File[] subFiles = src.listFiles(); for(File f:subFiles){ if(f.isFile()){ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newDir,f.getName()))); int b; while((b = bis.read())!=-1){ bos.write(b); } bis.close(); bos.close(); } else { copy(f,newDir); } } } }
Print by hierarchy
From the keyboard structure to a folder path, print all the files in the folder and the names of the folder by level
1. Enter the file path with the keyboard to obtain all files and folders
2. Traverse the array
3. Both files and folders need to be printed directly
4. If it is a folder recursive call
import java.io.File; public class test4 { public static void main(String[] args) { File dir = test1.getDir(); int lev = 0; printName(dir,lev); } private static void printName(File dir,int lev) { File[] subFiles = dir.listFiles(); for(File f:subFiles) { for(int i = 0 ;i< lev ;i++) { System.out.print("\t"); } System.out.println(f.getName()); if(f.isDirectory()) { printName(f,lev+1); //++lev or lev + + will change the original value } } } }
Fibonacci sequence
Immortal rabbit
Suppose that a pair of newborn rabbits can grow into big rabbits in one month, and then give birth to a pair of rabbits in another month. There is no death in a year. How many pairs of rabbits can a pair of newborn rabbits breed in a year:
- A pair of little rabbits {1
- A pair of big rabbits and a pair of small rabbits 1
- Two pairs of big rabbits and one pair of small rabbits 2
- Three pairs of big rabbits, two pairs of small rabbits 3
- Five pairs of big rabbits, three pairs of small rabbits 5
- Eight pairs of big rabbits, five pairs of small rabbits 8
We can get the rule that the number of big rabbits in this month is equal to the sum of the number of big and small rabbits in last month, and the number of small rabbits in this month is equal to the number of big rabbits in last month.
code implementation
public class test5 { public static void main(String[] args) { System.out.println(fun(8)); } public static int fun(int num) { //Recursive approach if(num == 1 || num==2){ return 1; } else { return fun(num-1)+fun(num-2); } } public static void arrtest() { //Array approach int[] arr = new int[8]; arr[0] = 1; arr[1] = 1; for(int i = 2;i<arr.length;i++){ arr[i] = arr[i-2] +arr[i-1]; } } }
The number of all zeros and tail zeros of the factorial of 1000
Non recursive:
import java.math.BigInteger; public class test6 { public static void main(String[] args) { BigInteger bi1 = new BigInteger("1"); for(int i = 1; i<=1000;i++) { //Get the result of 1000 factorial BigInteger bi2 = new BigInteger(i + ""); bi1 = bi1.multiply(bi2); } String str = bi1.toString(); int c = 0; for(int i = 0;i<str.length();i++){ //Find the number of all zeros if('0' == str.charAt(i)) { c++; } } System.out.println(c); StringBuilder sb = new StringBuilder(str); //Gets the number of zeros in the tail str = sb.reverse().toString(); int c2 = 0; for(int i = 0;i<str.length();i++){ if('0' != str.charAt(i)){ break; } else { c2++; } } System.out.println(c2); } }
Recursive writing
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100... 1000 ﹐ 1000 / 5 = a multiple of 200 ﹐ 5 ﹐ produces at least one 0 ﹐ these numbers are multiplied by any even number and will end in 0
5*5 5*5*2 5*5*3 5*5*4 5*5*5 5*5*6 5*5*7 5*5*8 5*5*9 5*5*10... 5 * 5 * 40 ^ 200 / 5 = multiples of 40 ^ 25 ^ generate at least two zeros ^ the first time will generate a 0, all of which are counted, but some numbers will generate two zeros. Remove one 0 and there is one 0 left
5 * 5 * 5 * 5 * 5 * 2 5 * 5 * 5 * 3 5 * 5 * 5 * 4 5 * 5 * 5 * 5 * 5 * 6 5 * 5 * 5 * 7 5 * 5 * 5 * 8 * 40 / 5 = multiples of 8 * 125 * generate at least 3 zeros * remove 2 zeros and leave 1 0
5 * 5 * 5 * 5 * 8 / 5 = multiple of 1 625 * 4 zeros * remove 3 zeros and leave 1 0
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); } }
Joseph Ring
All people form a circle, count to three and three multiples, and the winner is the winner
import java.util.ArrayList; public class test7 { public static void main(String[] args) { System.out.println(getLuckyNum(8)); } public static int getLuckyNum(int num) { ArrayList<Integer> list = new ArrayList<>(); //Objects that store 1 to num for(int i = 1;i<=num;i++){ list.add(i); } int count = 1; for(int i = 0; list.size() != 1;i++) { if(i == list.size()) { //So you can form a circle i = 0; } if(count % 3 == 0){ //If it is a multiple of 3, it will be eliminated list.remove(i); i--; //Otherwise, it will go to the next one, and one will be missed in the middle } count++; } return list.get(0); } }