本文目錄一覽:
- 1、java中遞歸算法是怎麼算的?
- 2、用java遞歸方法實現
- 3、JAVA遞歸算法,1^2+2^2+3^2+….+n^2
- 4、JAVA中的遞歸方法,求講一下。
- 5、java遞歸算法的例子。
- 6、請問大家java中遞歸算法,希望有詳細解釋
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遞歸方法實現
1、遞歸做為一種算法在程序設計語言中廣泛使用,是指函數/過程/子程序在運行過程中直接或間接調用自身而產生的重入現象。
2、遞歸算法一般用於解決三類問題:
1)數據的定義是按遞歸定義的。(Fibonacci(斐波那契)的函數)
2)問題解法按遞歸算法實現。(回溯)
3)數據的結構形式是按遞歸定義的。(樹的遍歷,圖的搜索)
JAVA遞歸算法,1^2+2^2+3^2+….+n^2
publicclassDigui{//遞歸算法:publicstaticdoubledigui(intn){doublesum=0.0;if(n==1){sum=1;}else{if(n%2==0){sum=1.0/n+digui(n-1);}else{sum=-1.0/n+digui(n-1);}}returnsum;}//非遞歸算法:publicstaticdoublefeidigui(intn){doublecount=0.0;StringBuffersb=newStringBuffer();for(doublei=1;i=n;i++){if(i==1){count=1;sb.append(“n!=”+n+”!=1″);}else{if(i%2==0){count=count+1/i;sb.append(“+1/”+(int)i);}else{count=count-1/i;sb.append(“-1/”+(int)i);}}}System.err.println(sb.toString());returncount;}publicstaticvoidmain(String[]args){intn=10;doubledigui=feidigui(n);doublefeidigui=digui(n);System.out.println(“遞歸算法:”+n+”!=”+digui);System.out.println(“非遞歸算法:”+n+”!=”+feidigui);}}運行結果如下:n!=10!=1+1/2-1/3+1/4-1/5+1/6-1/7+1/8-1/9+1/10遞歸算法:10!=1.3543650793650797非遞歸算法:10!=1.3543650793650797
JAVA中的遞歸方法,求講一下。
方法遞歸和循環語句差不多,打個比喻。方法遞歸是小明上樓拿東西,一樓,二樓,三樓……樓頂。在樓頂拿到想要的東西以後,你總不能直接跳下來吧。你得一層一層的返回下來。循環就是驢拉磨,你轉多少圈都是在原地。變化的只是盤子里的東西有變化。方法遞歸不會進入死循環,但陷的太深系統會崩潰。
答得不好抱歉
java遞歸算法的例子。
階乘:
要求:給定一個數值,計算出它的階乘值,例如5的階乘為5*4*3*2*1
實現:
[html] view plaincopy
span style=”font-size:12px;” // 利用遞歸實現一個數的階乘值 private static BigDecimal getNum(BigDecimal inNum) { if (inNum.compareTo(BigDecimal.ONE) == 0) { return inNum; } return inNum.multiply(getNum(inNum.subtract(BigDecimal.ONE))); }/span
(2)Fibonacci數列:1,1,2,3,5,8,13……
要求:找出數列中指定index位置的數值
實現:
[html] view plaincopy
span style=”font-size:12px;” // 利用遞歸實現了Fibonacci數列 private static int fab(int index) { if (index == 1 || index == 2) { return 1; } else { return fab(index – 1) + fab(index – 2); } }/span
(3)漢諾塔
要求:漢諾塔挪動
實現:
[html] view plaincopy
span style=”font-size:12px;” span style=”white-space:pre;” /spanprivate static final String DISK_B = “diskB”; span style=”white-space:pre;” /spanprivate static final String DISK_C = “diskC”; span style=”white-space:pre;” /spanprivate static final String DISK_A = “diskA”; span style=”white-space:pre;” /spanstatic String from=DISK_A; span style=”white-space:pre;” /span static String to=DISK_C; span style=”white-space:pre;” /span static String mid=DISK_B; span style=”white-space:pre;” /span public static void main(String[] args) { span style=”white-space:pre;” /span String input=JOptionPane.showInputDialog(“please input the number of the disks you want me move.”); span style=”white-space:pre;” /span int num=Integer.parseInt(input); span style=”white-space:pre;” /span move(num,from,mid,to); span style=”white-space:pre;” /span }/span
[html] view plaincopy
span style=”font-size:12px;” // 利用遞歸實現漢諾塔 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); } }/span
(4)排列組合
要求:將輸入的一個字符串中的所有元素進行排序並輸出,例如:你給出的參數是”abc”,
則程序會輸出
abc
acb
bac
bca
cab
cba
實現:
[html] view plaincopy
span style=”font-size:12px;”span style=”white-space:pre;” /spanpublic static void permute(String str) { span style=”white-space:pre;” /span char[] strArray = str.toCharArray(); span style=”white-space:pre;” /span permute(strArray, 0, strArray.length – 1); span style=”white-space:pre;” /span}/span
[html] view plaincopy
span style=”font-size:12px;” // 利用遞歸實現,將輸入的一個字符串中的所有元素進行排序並輸出 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];
請問大家java中遞歸算法,希望有詳細解釋
public class Test{
public static int result(int parameter){
if(parameter=1) return 1; // 判斷parameter是否小於等於1,如果不成立則遞歸調用
int number = parameter*result(parameter-1); // 將parameter-1繼續調用函數 反覆如此,直至條件成立。
return number;
}
public static void main(String[]args{
int result = result(5);
System.out.println(result);
}
}
它的執行原理是如下這樣的:
result(5) 初始時 ==》進入函數體判斷parameter是否小於等於1,此時parameter等於5,條件不成立,執行parameter*result(parameter-1) 即5*result(5-1),程序反覆執行。。。
5*result(5-1)
4*result(4-1)
3*result(3-1)
2*result(2-1) 到此 parameter等於1符合條件 函數返回1,層層返回。即:
result(1) =1
2*result(1)=2*1=2
3*result(2)=3*2=6
4*result(3)=4*6=24
5*result(4)=5*24=120
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246968.html