日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【算法篇】递归

發(fā)布時(shí)間:2025/3/8 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【算法篇】递归 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、遞歸的概念

程序調(diào)用自身的編程技巧稱(chēng)為遞歸。

遞歸的核心思想就是將一個(gè)大規(guī)模復(fù)雜的問(wèn)題層層轉(zhuǎn)化為一個(gè)與原問(wèn)題相似的規(guī)模較小的問(wèn)題來(lái)求解。

二、遞歸的優(yōu)點(diǎn)

使用遞歸的好處是只需要少量的代碼就可以描述出求解問(wèn)題過(guò)程中多次重復(fù)的計(jì)算,大大減少了程序的代碼量。

三、遞歸的缺點(diǎn)

在時(shí)間和空間的復(fù)雜度上往往不是最優(yōu)的。

四、實(shí)現(xiàn)遞歸的條件

一般來(lái)說(shuō),遞歸要有邊界、遞歸前進(jìn)段和遞歸返回段。當(dāng)不滿足邊界條件時(shí),遞歸前進(jìn),當(dāng)滿足邊界條件時(shí),遞歸返回。

五、示例

使用遞歸將一個(gè)文件夾中的所有文件拷貝到另外一個(gè)文件夾

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.io.InputStream; import java.io.OutputStream;/** 使用遞歸將一個(gè)文件夾中的所有文件拷貝到另外一個(gè)文件夾*/ public class Recursion {public final String srcPath;public final String targetPath;public Recursion(String srcPath, String targetPath) {this.srcPath = srcPath;this.targetPath = targetPath;}public String getSrcPath() {return srcPath;}public String getTargetPath() {return targetPath;}public static void main(String[] args) throws IOException {Recursion recursion = new Recursion("F:\\固態(tài)盤(pán)之前\\MyWorkSpace\\MyEclipse 9\\day13","C:\\Users\\cc\\Desktop\\day13");recursion.iteratorFile(recursion.getSrcPath());}/** 遞歸方法*/public void iteratorFile(String srcPath) {if (srcPath != null && !srcPath.equals("")) {File file = new File(srcPath);if (file.isDirectory()) {File[] files = file.listFiles();//遍歷當(dāng)前目錄下的所有文件和目錄for (int i = 0; i < files.length; i++) {if (files[i].isDirectory()) {iteratorFile(files[i].getAbsolutePath());} else {//開(kāi)啟一個(gè)線程拷貝文件new ThreadCopy(files[i].getAbsolutePath(),files[i].getAbsolutePath().replace(this.getSrcPath(), this.getTargetPath())).start();}}} else {//開(kāi)啟一個(gè)線程拷貝文件new ThreadCopy(file.getAbsolutePath(),file.getAbsolutePath().replace(this.getSrcPath(), this.getTargetPath())).start();}}} }class ThreadCopy extends Thread {private String src;private String tar;public ThreadCopy(String src, String tar) {this.src = src;this.tar = tar;}@Overridepublic void run() {FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;BufferedOutputStream bos = null;try {File file = new File(tar);//如果拷貝文件的目標(biāo)目錄不存在,則創(chuàng)建目標(biāo)目錄File directory = new File(file.getParent());if (!directory.exists()) {directory.mkdirs();}fis = new FileInputStream(src);bis = new BufferedInputStream(fis);fos = new FileOutputStream(tar);bos = new BufferedOutputStream(fos);byte[] buffer = new byte[1024];int len = 0;while ((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);}bos.flush();System.out.println(Thread.currentThread().getName() + ":拷貝完了!");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//關(guān)閉流close(bos, fos);close(bis, fis);}}public void close(OutputStream oStream1, OutputStream oStream2) {if (oStream1 != null) {try {oStream1.close();} catch (IOException e) {e.printStackTrace();}}if (oStream2 != null) {try {oStream2.close();} catch (IOException e) {e.printStackTrace();}}}public void close(InputStream iStream1, InputStream iStream2) {if (iStream1 != null) {try {iStream1.close();} catch (IOException e) {e.printStackTrace();}}if (iStream2 != null) {try {iStream2.close();} catch (IOException e) {e.printStackTrace();}}}}

?

?

總結(jié)

以上是生活随笔為你收集整理的【算法篇】递归的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。