递归练习:
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Teat1 { public static void main(String[] args) throws IOException { // 从键盘接收一个文件夹路径,统计该文件夹大小 // 文件夹内容删除 // File dir = getFir(); // System.out.println(getFileLength(dir)); File src = getFir(); // File dest = getFir(); // if (src.equals(dest)) { // System.out.println("目标文件夹是源文件夹得子文件夹"); // }else { // copy(src,dest); // } printLev(src, 0); } public static void printLev(File dir,int lev) { File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { for (int i = 0; i <= lev; i++) { System.out.print(" "); } System.out.println(subFile); if (subFile.isDirectory()) { printLev(subFile, lev+1); // 不能为++lev或lev++,会改变lev 值 } } } public static void copy(File src, File dest) throws IOException { // 一个文件夹中(包含内容)拷贝到另一个文件夹中 File newDir = new File(dest,src.getName()); newDir.mkdir(); File[] subFiles = src.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 b; while((b=bis.read())!=-1) { bos.write(b); } bis.close(); bos.close(); }else { // 文件夹就递归调用 copy(subFile, newDir); } } } public static File getFir() { while(true) { Scanner scanner = new Scanner(System.in); System.out.println("请输入文件夹路径:"); String line = scanner.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 getFileLength(File dir) { long len = 0; File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { if (subFile.isFile()) { len = len + subFile.length(); }else { len = len + getFileLength(subFile); } } return len; } public static void delFile(File dir) { // 遍历删除文件,最后删除空的文件夹 File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { if (subFile.isFile()) { subFile.delete(); }else { delFile(subFile); } } dir.delete(); // 删除空文件夹 } }
public class Test2 { public static void main(String[] args) { // 斐波那契 // 1 1 2 3 5 8 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]; } System.out.println(arr[arr.length-1]); System.out.println(fun(8)); } public static int fun(int num) { if (num==1 || num==2) { return 1; }else { return fun(num-2) + fun(num-1); } } }
import java.math.BigInteger; public class Test3 { public static void main(String[] args) { // 1000的阶乘所有零和尾部零的个数 BigInteger bi1 = new BigInteger("1"); for (int i = 1; i <= 1000; i++) { BigInteger bi2 = new BigInteger(i+""); bi1 = bi1.multiply(bi2); //相乘 } String string = bi1.toString(); int count = 0; for (int i = 0; i < string.length(); i++) { if ('0'==string.charAt(i)) { // 索引 count++; } } System.out.println(count); //472 //尾部零的个数 //通过反转 StringBuffer stringBuffer = new StringBuffer(string); String string2 = stringBuffer.reverse().toString(); int count1 = 0; for (int i = 0; i < string2.length(); i++) { if ('0'!=string2.charAt(i)) { break; }else { count1++; } } System.out.println(count1); //249 System.out.println(fun(20)); } //尾部零的个数 // 2*5*10 *12*15*20 *22*25*30 // 1 2 3 4 5+1 6+1 public static int fun(int num) { if(num > 0 && num < 5) { return 0; }else { return num / 5 + fun(num / 5); } } }
import java.util.ArrayList; public class Test4 { 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; i++) { list.add(i); } int count = 1; for (int i = 0; list.size()!=1; i++) { //只要集合只留一个 if (i==list.size()) { //i增长到集合最大的索引+1时 i = 0; } if (count%3==0) { list.remove(i--); // 删除要更改i } count++; } return list.get(0); } }