Java基础23-递归练习
Created At :
Count:2.3k
Views 👀 :
public class Test1 {
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 68 69 70 71 72 73 74 75 76 77 78
| public class Test1 {
public static void main(String[] args) { File dir = getDir(); System.out.println(getFileLength(dir)); }
public static File getDir() { Scanner sc = new Scanner(System.in); System.out.println("请输入一个文件夹路径:"); while(true) { String line = sc.nextLine(); File dir = new File(line); if(!dir.exists()) { System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径:"); }else if(dir.isFile()) { System.out.println("您录入的是文件路径,请输入一个文件夹路径:"); }else { return dir; } } }
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; } }
|
Test2
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
| public class Test2 {
public static void main(String[] args) { File dir = Test1.getDir(); deleteFile(dir); }
public static void deleteFile(File dir) { File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { if(subFile.isFile()) { subFile.delete(); }else { deleteFile(subFile); } } dir.delete(); } }
|
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
| 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("目标文件夹是源文件夹的子文件夹"); }else { copy(src,dest); } }
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); } } } }
|
Test4
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
| public class Test4 {
public static void main(String[] args) { File dir = Test1.getDir(); printLev(dir,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("\t"); } System.out.println(subFile); if(subFile.isDirectory()) { printLev(subFile,++lev); } } }
}
|
Test5
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
| 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 = 2; i < arr.length; i++) { arr[i] = arr[i - 2] + arr[i - 1]; } System.out.println(arr[arr.length - 1]); }
public static int fun(int num) { if(num == 1 || num == 2) { return 1; }else { return fun(num - 2) + fun(num - 1); } } }
|
Test6
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
| public class Test6 {
public static void main(String[] args) {
demo2(); }
public static void demo2() { BigInteger bi1 = new BigInteger("1"); for(int i = 1; i <= 1000; i++) { BigInteger bi2 = new BigInteger(i+""); bi1 = bi1.multiply(bi2); } String str = bi1.toString(); StringBuilder sb = new StringBuilder(str); str = sb.reverse().toString(); int count = 0; for(int i = 0; i < str.length(); i++) { if('0' != str.charAt(i)) { break; }else { count++; } } System.out.println(count); }
public static void demo1() { BigInteger bi1 = new BigInteger("1"); for(int i = 1; i <= 1000; i++) { BigInteger bi2 = new BigInteger(i+""); bi1 = bi1.multiply(bi2); } String str = bi1.toString(); int count = 0; for(int i = 0; i < str.length(); i++) { if('0' == str.charAt(i)) { count++; } } System.out.println(count); }
}
|
Test7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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); } } }
|
Test8
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
| 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; 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); } }
|
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1210331079@qq.com
Title:Java基础23-递归练习
Count:2.3k
Author:千 羽
Created At:2020-06-16, 12:21:56
Updated At:2020-11-27, 09:02:29
Url:https://nateshao.github.io/2020/06/16/Java%E5%9F%BA%E7%A1%8023-%E9%80%92%E5%BD%92%E7%BB%83%E4%B9%A0/
Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.