23 遞迴練習
阿新 • • 發佈:2018-12-03
練習一
/** * 需求:1,從鍵盤接收一個資料夾路徑,統計該資料夾大小 * * 從鍵盤接收一個資料夾路徑 * 1,建立鍵盤錄入物件 * 2,定義一個無限迴圈 * 3,將鍵盤錄入的結果儲存並封裝成File物件 * 4,對File物件判斷 * 5,將資料夾路徑物件返回 * * 統計該資料夾大小 * 1,定義一個求和變數 * 2,獲取該資料夾下所有的檔案和資料夾listFiles(); * 3,遍歷陣列 * 4,判斷是檔案就計算大小並累加 * 5,判斷是資料夾,遞迴呼叫 */ public static void main(String[] args) { //File dir = new File("F:\\day06"); //System.out.println(dir.length()); //直接獲取資料夾的結果是0 File dir = getDir(); System.out.println(getFileLength(dir)); } /* * 從鍵盤接收一個資料夾路徑 * 1,返回值型別File * 2,引數列表無 */ public static File getDir() { //1,建立鍵盤錄入物件 Scanner sc = new Scanner(System.in); System.out.println("請輸入一個資料夾路徑:"); //2,定義一個無限迴圈 while(true) { //3,將鍵盤錄入的結果儲存並封裝成File物件 String line = sc.nextLine(); File dir = new File(line); //4,對File物件判斷 if(!dir.exists()) { System.out.println("您錄入的資料夾路徑不存在,請輸入一個資料夾路徑:"); }else if(dir.isFile()) { System.out.println("您錄入的是檔案路徑,請輸入一個資料夾路徑:"); }else { //5,將資料夾路徑物件返回 return dir; } } } /* * 統計該資料夾大小 * 1,返回值型別long * 2,引數列表File dir */ public static long getFileLength(File dir) { //dir = F:\day06\day07 //1,定義一個求和變數 long len = 0; //2,獲取該資料夾下所有的檔案和資料夾listFiles(); File[] subFiles = dir.listFiles(); //day07 Demo1_Student.class Demo1_Student.java //3,遍歷陣列 for (File subFile : subFiles) { //4,判斷是檔案就計算大小並累加 if(subFile.isFile()) { len = len + subFile.length(); //5,判斷是資料夾,遞迴呼叫 }else { len = len + getFileLength(subFile); } } return len; }
練習二
/** * 需求:2,從鍵盤接收一個資料夾路徑,刪除該資料夾 * * 刪除該資料夾 * 分析: * 1,獲取該資料夾下的所有的檔案和資料夾 * 2,遍歷陣列 * 3,判斷是檔案直接刪除 * 4,如果是資料夾,遞迴呼叫 * 5,迴圈結束後,把空資料夾刪掉 */ public static void main(String[] args) { File dir = Test1.getDir(); //獲取資料夾路徑 deleteFile(dir); } /* * 刪除該資料夾 * 1,返回值型別 void * 2,引數列表File dir */ public static void deleteFile(File dir) { //1,獲取該資料夾下的所有的檔案和資料夾 File[] subFiles = dir.listFiles(); //2,遍歷陣列 for (File subFile : subFiles) { //3,判斷是檔案直接刪除 if(subFile.isFile()) { subFile.delete(); //4,如果是資料夾,遞迴呼叫 }else { deleteFile(subFile); } } //5,迴圈結束後,把空資料夾刪掉 dir.delete(); }
練習三
/** * 需求:3,從鍵盤接收兩個資料夾路徑,把其中一個資料夾中(包含內容)拷貝到另一個資料夾中 * * 把其中一個資料夾中(包含內容)拷貝到另一個資料夾中 * 分析: * 1,在目標資料夾中建立原資料夾 * 2,獲取原資料夾中所有的檔案和資料夾,儲存在File陣列中 * 3,遍歷陣列 * 4,如果是檔案就用io流讀寫 * 5,如果是資料夾就遞迴呼叫 * @throws IOException */ 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); } } /* * 把其中一個資料夾中(包含內容)拷貝到另一個資料夾中 * 1,返回值型別void * 2,引數列表File src,File dest */ public static void copy(File src, File dest) throws IOException {//src原始檔夾,dest目標資料夾 //1,在目標資料夾中建立原資料夾 File newDir = new File(dest, src.getName());//在資料夾dest中建立 src.getName()資料夾 newDir.mkdir(); //2,獲取原資料夾中所有的檔案和資料夾,儲存在File陣列中 File[] subFiles = src.listFiles(); //3,遍歷陣列 for (File subFile : subFiles) { //4,如果是檔案就用io流讀寫 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(); //5,如果是資料夾就遞迴呼叫 }else { copy(subFile,newDir); } } }
練習四
/**
* 需求:4,從鍵盤接收一個資料夾路徑,把資料夾中的所有檔案以及資料夾的名字按層級列印, 例如:
* 把資料夾中的所有檔案以及資料夾的名字按層級列印
* 分析:
* 1,獲取所有檔案和資料夾,返回的File陣列
* 2,遍歷陣列
* 3,無論是檔案還是資料夾,都需要直接列印
* 4,如果是資料夾,遞迴呼叫
* day07
* day08
* xxx.jpg
* yyy.txt
* Demo1_Consturctor.class
* Demo1_Consturctor.java
* Demo1_Student.class
* Demo1_Student.java
*/
public static void main(String[] args) {
File dir = Test1.getDir(); //獲取資料夾路徑
printLev(dir,0);
}
public static void printLev(File dir,int lev) {
//1,把資料夾中的所有檔案以及資料夾的名字按層級列印
File[] subFiles = dir.listFiles();
//2,遍歷陣列
for (File subFile : subFiles) {
for(int i = 0; i <= lev; i++) {
System.out.print("\t");
}
//3,無論是檔案還是資料夾,都需要直接列印
System.out.println(subFile);
//4,如果是資料夾,遞迴呼叫
if(subFile.isDirectory()) {
//printLev(subFile,lev + 1);
printLev(subFile,++lev);
}
}
}
練習五
/**
* * 不死神兔
* 故事得從西元1202年說起,話說有一位義大利青年,名叫斐波那契。
* 在他的一部著作中提出了一個有趣的問題:假設一對剛出生的小兔一個月後就能長成大兔,再過一個月就能生下一對小兔,並且此後每個月都生一對小兔,一年內沒有發生死亡,
* 問:一對剛出生的兔子,一年內繁殖成多少對兔子?
* 1 1 2 3 5 8 13 21
* 1 = fun(1)
* 1 = fun(2)
* 2 = fun(1) + fun(2)
* 3 = fun(2) + fun(3)
*/
public static void main(String[] args) {
//demo1();
System.out.println(fun(8));
}
public static void demo1() {
//用陣列做不死神兔
int[] arr = new int[8];
//陣列中第一個元素和第二個元素都為1
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);
}
}
練習六
/**
* @param args
* 需求:求出1000的階乘所有零和尾部零的個數,不用遞迴做
*/
public static void main(String[] args) {
/*int result = 1;
for(int i = 1; i <= 1000; i++) {
result = result * i;
}
System.out.println(result); //0,因為1000的階乘遠遠超出了int的取值範圍
*/
//demo1();
demo2();
}
public static void demo2() { //獲取1000的階乘尾部有多少個零
BigInteger bi1 = new BigInteger("1");
for(int i = 1; i <= 1000; i++) {
BigInteger bi2 = new BigInteger(i+"");
bi1 = bi1.multiply(bi2); //將bi1與bi2相乘的結果賦值給bi1
}
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() { //求1000的階乘中所有的零
BigInteger bi1 = new BigInteger("1");
for(int i = 1; i <= 1000; i++) {
BigInteger bi2 = new BigInteger(i+"");
bi1 = bi1.multiply(bi2); //將bi1與bi2相乘的結果賦值給bi1
}
String str = bi1.toString(); //獲取字串表現形式
int count = 0;
for(int i = 0; i < str.length(); i++) {
if('0' == str.charAt(i)) { //通過索引獲取字元
count++; //如果字串中出現了0字元,計數器加1
}
}
System.out.println(count);
}
練習七
/**
* @param args
* 約瑟夫環
* * 幸運數字
*/
public static void main(String[] args) {
System.out.println(getLucklyNum(8));
}
/*
* 獲取幸運數字
* 1,返回值型別int
* 2,引數列表int num
*/
public static int getLucklyNum(int num) {
ArrayList<Integer> list = new ArrayList<>(); //建立集合儲存1到num的物件
for(int i = 1; i <= num; i++) {
list.add(i); //將1到num儲存在集合中
}
int count = 1; //用來數數的,只要是3的倍數就殺人
for(int i = 0; list.size() != 1; i++) { //只要集合中人數超過1,就要不斷的殺
if(i == list.size()) { //如果i增長到集合最大的索引+1時
i = 0; //重新歸零
}
if(count % 3 == 0) { //如果是3的倍數
list.remove(i--); //殺了一個人後,索引要減1,要不然會漏掉一個人
}
count++;
}
return list.get(0);
}