本文目錄一覽:
java中遞歸算法是怎麼算的?
1.漢諾塔問題
import javax.swing.JOptionPane;
public class Hanoi {
private static final String DISK_B = “diskB”;
private static final String DISK_C = “diskC”;
private static final String DISK_A = “diskA”;
static String from=DISK_A;
static String to=DISK_C;
static String mid=DISK_B;
public static void main(String[] args) {
String input=JOptionPane.showInputDialog(“please input the number of the disks you want me move.”);
int num=Integer.parseInt(input);
move(num,from,mid,to);
}
private static void move(int num, String from2, String mid2, String to2) {
if(num==1){
System.out.println(“move disk 1 from “+from2+” to “+to2);
}
else {
move(num-1,from2,to2,mid2);
System.out.println(“move disk “+num+” from “+from2+” to “+to2);
move(num-1,mid2,from2,to2);
}
}
}
2. 這是一個排列的例子,它所做的工作是將輸入的一個字符串中的所有元素進行排序並輸出,例如:你給出的參數是”abc” 則程序會輸出:
abc
acb
bac
bca
cab
cba
(1)算法的出口在於:low=high也就是現在給出的排列元素只有一個時。
(2)算法的逼近過程:先確定排列的第一位元素,也就是循環中i所代表的元素,
然後low+1開始減少排列元素,如此下去,直到low=high
public static void permute(String str) {
char[] strArray = str.toCharArray();
permute(strArray, 0, strArray.length – 1);
}
public static void permute(char[] list, int low, int high) {
int i;
if (low == high) {
String cout = “”;
for (i = 0; i= high; i++)
cout += list[i];
System.out.println(cout);
} else {
for (i = low; i= high; i++) {
char temp = list[low];
list[low] = list[i];
list[i] = temp;
permute(list, low + 1, high);
temp = list[low];
list[low] = list[i];
list[i] = temp;
}
}
}
3。這是一個組合的例子,與上述的例子相似,只是它所做的工作是,輸出所給字符串中制定數目的元素的組合種類
(1)程序出口在於n=1,此時只要輸出目標數組的所有元素即可
(2)逼近過程,當n1 的時候,我們先取第一個元素放入目標數組中,然後n-1,如此下去,最後出來。
import javax.swing.JOptionPane;
public class Combination {
/**
* @param args
*/
public static void main(String[] args) {
String input = JOptionPane.showInputDialog(“please input your String: “);
String numString = JOptionPane.showInputDialog(“please input the number of your Combination: “);
int num = Integer.parseInt(numString);
Combine(input, num);
}
private static void Combine(String input, int num) {
char[] a = input.toCharArray();
String b = “”;
Combine(a, num, b, 0, a.length);
}
private static void Combine(char[] a, int num, String b, int low, int high) {
if (num == 0) {
System.out.println(b);
} else {
for (int i = low; ia.length; i++) {
b += a[i];
Combine(a, num – 1, b, i+1, a.length);
b=b.substring(0, b.length()-1);
}
}
}
}
JAVA中的遞歸方法,求講一下。
方法遞歸和循環語句差不多,打個比喻。方法遞歸是小明上樓拿東西,一樓,二樓,三樓……樓頂。在樓頂拿到想要的東西以後,你總不能直接跳下來吧。你得一層一層的返回下來。循環就是驢拉磨,你轉多少圈都是在原地。變化的只是盤子里的東西有變化。方法遞歸不會進入死循環,但陷的太深系統會崩潰。
答得不好抱歉
用java遞歸方法實現
1、遞歸做為一種算法在程序設計語言中廣泛使用,是指函數/過程/子程序在運行過程中直接或間接調用自身而產生的重入現象。
2、遞歸算法一般用於解決三類問題:
1)數據的定義是按遞歸定義的。(Fibonacci(斐波那契)的函數)
2)問題解法按遞歸算法實現。(回溯)
3)數據的結構形式是按遞歸定義的。(樹的遍歷,圖的搜索)
在JAVA中什麼是遞歸?有什麼用?
Java方法遞歸是指在一個方法的內部調用自身的過程,以此類推就是java方法遞歸的理解思想,具體來講就是把規模大的問題轉化為規模小的相似的子問題來解決。在函數實現時,因為解決大問題的方法和解決小問題的方法往往是同一個方法,所以就產生了函數調用它自身的情況。另外這個解決問題的函數必須有明顯的結束條件,這樣就不會產生無限遞歸的情況了。因此,java方法遞歸的兩個條件就是,一通過遞歸調用來縮小問題規模,且新問題與原問題有着相同的形式;二存在一種簡單情境,可以使遞歸在簡單情境下退出。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/153634.html