Java recursion exercises

1. Receive a folder path from the keyboard and count the size of the folder

package demo;

import java.io.File;
import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		File file = getDirectory();
		System.out.println(fileSize(file));
	}

	// Determine whether it is a folder
	public static File getDirectory() {
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter the file path:");

		while (true) {
			String path = sc.next();
			File file = new File(path);
			if (!file.exists()) {
				System.out.print("File does not exist, please re-enter:");
			} else if (file.isFile()) {
				System.out.print("You entered the file path,Please re-enter:");
			} else {
				return file;
			}
		}

	}

	// Use recursion to determine folder size
	public static int fileSize(File file) {

		int size = 0;

		File[] files = file.listFiles();

		for (File file1 : files) {
			if (file1.isFile()) {
				size += file1.length();
			} else {
				size += fileSize(file1);
			}
		}

		return size;
	}

}

2. Receive a folder path from the keyboard and delete the folder

package demo;

import java.io.File;
import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		File file = getDirectory();
		deleteDirectory(file);
		if (file.exists()) {
			System.out.println("Delete failed");
		} else {
			System.out.println("Delete successful");
		}
	}

	// Determine whether it is a folder
	public static File getDirectory() {
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter the file path:");

		while (true) {
			String path = sc.next();
			File file = new File(path);
			if (!file.exists()) {
				System.out.print("File does not exist, please re-enter:");
			} else if (file.isFile()) {
				System.out.print("You entered the file path,Please re-enter:");
			} else {
				return file;
			}
		}

	}

	// Use recursion to delete folders
	public static void deleteDirectory(File file) {

		File[] files = file.listFiles();

		for (File file1 : files) {
			if (file1.isFile()) {
				file1.delete();
			} else {
				deleteDirectory(file1);
			}
		}

		// Delete files when there are no files in the folder
		file.delete();
	}

}

3. Receive two folder paths from the keyboard, and copy one folder (including content) to the other

package demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Test3 {

	public static void main(String[] args) throws IOException {
		File src = getDirectory();
		File dest = getDirectory();
		if (src.equals(dest)) {
			System.out.println("The destination folder is a subfolder of the original folder");
		} else {
			copyFile(src, dest);
		}

	}

	// Determine whether it is a folder
	public static File getDirectory() {
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter the file path:");

		while (true) {
			String path = sc.next();
			File file = new File(path);
			if (!file.exists()) {
				System.out.print("File does not exist, please re-enter:");
			} else if (file.isFile()) {
				System.out.print("You entered the file path,Please re-enter:");
			} else {
				return file;
			}
		}

	}

	// Using recursive copy folders
	public static void copyFile(File src, File dest) throws IOException {

		// First create the original folder in the destination folder
		File newFile = new File(dest, src.getName());
		newFile.mkdir();

		// Traverse files and folders in the original file
		File[] files = src.listFiles();

		for (File file1 : files) {
			if (file1.isFile()) {
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
				BufferedOutputStream bos = new BufferedOutputStream(
						new FileOutputStream(new File(newFile, file1.getName())));
				int b;
				while ((b = bis.read()) != -1) {
					bos.write(b);
				}
				bis.close();
				bos.close();
			} else {
				copyFile(file1, newFile);
			}
		}
	}

}

4. Receive a folder path from the keyboard, and print all the files in the folder and the folder name by level

package demo;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Test4 {

	public static void main(String[] args) throws IOException {
		File file = getDirectory();
		tierPrint(file, 0);
	}

	// Determine whether it is a folder
	public static File getDirectory() {
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter the file path:");

		while (true) {
			String path = sc.next();
			File file = new File(path);
			if (!file.exists()) {
				System.out.print("File does not exist, please re-enter:");
			} else if (file.isFile()) {
				System.out.print("You entered the file path,Please re-enter:");
			} else {
				return file;
			}
		}

	}

	// Print hierarchically using recursion
	public static void tierPrint(File file, int lev) throws IOException {

		File[] files = file.listFiles();

		for (File file1 : files) {
			for (int i = 0; i < lev; i++) {
				System.out.print("\t");
			}
			System.out.println(file1.getPath());
			if (file1.isDirectory()) {
				tierPrint(file1, lev + 1);
			}
		}
	}

}

5. Suppose that a pair of new born rabbits can grow into a big one in one month, and then a pair of small rabbits can be born in another month. After that, a pair of small rabbits will be born every month, and there will be no death within one year,
Q: how many pairs of new born rabbits can be bred in one year?
Rule: 1 1 2 3 5 8 13 21

package demo;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {

		// Rule: 1 1 2 3 5 8 13 21
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter the number of days:");
		int day = sc.nextInt();
		System.out.println(recursion(day));
		// System.out.println(array(day));
	}

	// Use recursion for calculation
	public static int recursion(int day) {

		if (day == 1 || day == 2) {
			return 1;
		} else {
			return recursion(day - 2) + recursion(day - 1);
		}

	}

	// Calculated by array
	public static int array(int day) {

		int[] is = new int[day];

		is[0] = 1;
		is[1] = 1;

		for (int i = 2; i < is.length; i++) {
			is[i] = is[i - 2] + is[i - 1];
		}
		return is[is.length - 1];
	}

}

6. Joseph Ring

N people form a circle, and report from the first one. Every third person will be killed, and the last one will be left, and the rest will be killed.

package demo;

import java.util.ArrayList;

public class Test6 {

	public static void main(String[] args) {
		System.out.println(getLucklyNum(8));// 7
	}

	public static int getLucklyNum(int num) {

		ArrayList<Integer> list = new ArrayList<Integer>();
		for (int i = 1; i <= num; i++) {
			list.add(i);
		}
		// count
		int count = 1;
		for (int i = 0; list.size() != 1; i++) {

			// If the subscript exceeds the maximum subscript, it is equivalent to another call from a person
			if (i == list.size()) {
				i = 0;
			}

			// Delete one for every three people
			if (count % 3 == 0) {
				list.remove(i);
				i--;
			}

			count++;

		}

		// The last one back
		return list.get(0);
	}

}

 

Published 25 original articles, won praise 13, visited 30000+
Private letter follow

Tags: Java REST

Posted on Wed, 12 Feb 2020 10:37:21 -0500 by gio2k