java递归算法的例子。

// 利用递归实现汉诺塔 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....
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;"> </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 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;"> </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 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];
2018-04-02
十进制整数转二进制字符串的递归写法:

public String dtob(int n) {
if (n == 0 || n == 1) {
return Integer.toString(n);
} else {
return dtob(n / 2) + Integer.toString(n % 2);
}
}2018-01-05
n!=n*(n-1)*(n-2)*......*2*1等同于n!=n*(n-1)! 其中n!与(n-1)!
相当于F(n)=n * F(n-1);所以F(n)方法如下:
public int F(int n){
if(n == 0){
return 1;
}
return n * F(n - 1);
}2018-01-05
mengvlog 阅读 35 次 更新于 2025-09-11 06:15:40 我来答关注问题0
  •  爱编程的小鹤 Java用递归实现3.根据规律写出计算算法:1、7、8、15、23、38、61.…求第28位

    } else { // 递归调用 return getNumber(n - 1) + getNumber(n - 2);} } }

  • // 利用递归实现汉诺塔 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....

  • 在使用Java进行递归输出杨辉三角时,可以通过编写一个方法来实现。这里有一个简单的例子:public static int f(int a, int b) { if (a == b || b == 1) return 1;return f(a - 1, b - 1) + f(a - 1, b);} 这个方法用于计算杨辉三角中特定位置的数。如果坐标是从1开始输入的...

  •  深空游戏 java二分法查找的递归算法怎么实现

    return mid;整个算法通过不断缩小查找范围,最终找到目标值的索引或者确认目标值不存在。} } 在主方法中,我们调用这个递归方法并打印结果。int a = binary(arr, 9, 0, arr.length - 1);System.out.println("被查找数字索引位置在:" + a);这将输出数字9在数组中的索引,如果没有找到则输出-1。

  • 【5】在做递归算法的时候,一定把握出口,也就是做递归算法必须要有一个明确的递归结束条件。这一点是非常重要的。其实这个出口就是一个条件,当满足了这个条件的时候我们就不再递归了。三、代码示例:代码执行流程图如下:此程序中n=5就是程序的出口。Java是一种可以撰写跨平台应用程序的面向对象的...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部