java中的遞歸演算法
❶ 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遞歸演算法,怎麼理解
n! = (n-1)*n!
簡單理解,就是目前的所有任務,等於前面所有的任務+現在的任務。
比如你求 1。。。100的加法總和
實際上是 1... 99 的加法總和 + 100 就是了。
這就是遞歸的來源。
你只需要計算你前一步的任務,然後加上自己,就OK了。
前一步,在再次調用前前一步......
❸ 用java遞歸方法實現
publicintfun(intn){
if(n==0||n==1)return1;
returnn*fun(n-1);
}
❹ 在JAVA中什麼是遞歸有什麼用
Java方法遞歸是指在一個方法的內部調用自身的過程,以此類推就是java方法遞歸的理解思想,具體來講就是把規模大的問題轉化為規模小的相似的子問題來解決。在函數實現時,因為解決大問題的方法和解決小問題的方法往往是同一個方法,所以就產生了函數調用它自身的情況。另外這個解決問題的函數必須有明顯的結束條件,這樣就不會產生無限遞歸的情況了。因此,java方法遞歸的兩個條件就是,一通過遞歸調用來縮小問題規模,且新問題與原問題有著相同的形式;二存在一種簡單情境,可以使遞歸在簡單情境下退出。
❺ java遞歸演算法的例子
簡單理解一下:
遞歸就是調用上一步的結果,來產生下一步的結果,一般上一步的結果是由函數得到,所以下一步直接調用函數,參數不一樣而已:
階乘演算法:
public int JieCheng(int n){
return n*JieCheng(n-1);
}
當然還必要一些條件判斷嗎,就是終止遞歸調用的,jiecheng為n=1 時 就return 1;
❻ 用java遞歸演算法求一個數字的階乘
用遞歸演算法求一個數字的階乘的程序如下:
public class JieCheng {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("請輸入一個整數:");
int n = in.nextInt();
System.out.println(n+"!="+f(n));
}
static long f(int n){
if (n==1) return 1;
else return n*f(n-1);
}
}
運行結果:
請輸入一個整數:6
6!=720
❼ 在java中,用遞歸方法計算n的階乘。
用Java求鍵盤輸入的數的階乘n。(遞歸演算法)packagejiecheng; importjava.util.*; //導入java.util包中的所有類classrep{ publiclongrep(intn){ longi=0; if(n==0||n==1) i=1;
elsi=n*rep(n-1) returni; } } publicclassJie{ publicstaticvoidmain(String[]args){ intn; //此處定義要輸入的數Scanners= newScanner(System.in); //以下三行用於n的值得輸入System.out.print( "請輸入一個整數:"); n=s.nextInt(); repf= newrep(); System.out.println(n+"!="+f.rep(n)); } }
❽ java遞歸演算法的例子。
階乘:
要求:給定一個數值,計算出它的階乘值,例如5的階乘為5*4*3*2*1
實現:
[html] view plain
<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 plain
<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 plain
<span style="font-size:12px;"> <span style="white-space:pre;"> </span>private static final String DISK_B = "diskB"; <span style="white-space:pre;"> </span>private static final String DISK_C = "diskC"; <span style="white-space:pre;"> </span>private static final String DISK_A = "diskA"; <span style="white-space:pre;"> </span>static 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 plain
<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 plain
<span style="font-size:12px;"><span style="white-space:pre;"> </span>public 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 plain
<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遞歸演算法
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)逼近過程,當n>1的時候,我們先取第一個元素放入目標數組中,然後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; i < a.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)數據的結構形式是按遞歸定義的。(樹的遍歷,圖的搜索)