❶ java的遞歸方法
橫線填:d2b(n/2);
❷ java遞歸實現折中演算法
public class TestSort<O extends Comparable<O>> {
private O[] data;
public TestSort(O[] data)
{
this.data = data;
}
/**
* 遞歸折中演算法
*/
public int sortByReturn(int low, int high, O value)
{
int mid = (low+high)/2;
if(value.compareTo(data[mid])<0)
{
high = mid -1;
return sortByReturn(low,high,value);
}else if(value.compareTo(data[mid])>0)
{
low = mid + 1;
return sortByReturn(low,high,value);
}else
{
return mid;
}
}
/**
* 循環折中演算法
* @param args
*/
public int sortByWhile(O value)
{
if(value==null)
{
return 0;
}
int low = 0;
int high = data.length-1;
int mid ;
while(low<=high)
{
mid = (high+low)/2;
if(value.compareTo(data[mid])<0)
{
high = mid-1;
}else if(value.compareTo(data[mid])>0)
{
low = mid+1;
}else if(value.compareTo(data[mid])==0)
{
return mid;
}
}
return -1;
}
public static void main(String[] args)
{
Integer data[] = {1,2,5,7,9,33,43,45,66,78,93};
TestSort<Integer> ts = new TestSort<Integer>(data);
System.out.println("sort by while:"+ts.sortByWhile(5));
System.out.println("sort2 by while:"+ts.sortByReturn(0,data.length-1,33));
}
}
❸ 用java遞歸演算法,求1+2+4+8+~~~~~的和
即f(x)=f(x-1)*2;f(1)=1;......
public class Test4 {
public static void main(String[]args){
System.out.println(num(10));
}
public static int f(int x){
if(x == 1)return 1;
return f(x - 1)*2;
}
public static int num(int x){
if(x == 1)return f(1);
return num(x - 1)+f(x);
}
}
❹ 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遞歸演算法的例子。
階乘:
要求:給定一個數值,計算出它的階乘值,例如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中的遞歸方法,求講一下。
自己調用自己或幾個方法相互調用。
最經典的是求正整數階的演算法:
int fact(int i){
if(i<=1)return 1;
return fact(i-1)*i;
}
多數遞歸方法可以轉換成非遞歸方法。
一般同功能的非遞歸方法,執行效率要優於遞歸方法。但合理的使用遞歸方法,可以使代碼結構更清晰,更有可讀性,從而更方便維護。
❼ 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遞歸演算法的例子
十進制整數轉二進制字元串的遞歸寫法:
public String dtob(int n) {
if (n == 0 || n == 1) {
return Integer.toString(n);
} else {
return dtob(n / 2) + Integer.toString(n % 2);
}
}