java递归算法例子代码 java递归算法经典题目

java递归算法的例子。

阶乘:

成都创新互联公司主营平潭网站建设的网络公司,主营网站建设方案,app开发定制,平潭h5微信小程序开发搭建,平潭网站营销推广欢迎平潭等地区企业咨询

要求:给定一个数值,计算出它的阶乘值,例如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中递归算法是什么怎么算的?

一、递归算法基本思路:

Java递归算法是基于Java语言实现的递归算法。递归算法是一种直接或者间接调用自身函数或者方法的算法。递归算法实质是把问题分解成规模缩小的同类问题的子问题,然后递归调用方法表示问题的解。递归往往能给我们带来非常简洁非常直观的代码形式,从而使我们的编码大大简化,然而递归的思维确实跟我们的常规思维相逆的,通常都是从上而下的思维问题,而递归趋势从下往上的进行思维。

二、递归算法解决问题的特点:

【1】递归就是方法里调用自身。

【2】在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口。

【3】递归算法代码显得很简洁,但递归算法解题的运行效率较低。所以不提倡用递归设计程序。

【4】在递归调用的过程中系统为每一层的返回点、局部量等开辟了栈来存储。递归次数过多容易造成栈溢出等,所以一般不提倡用递归算法设计程序。

【5】在做递归算法的时候,一定把握出口,也就是做递归算法必须要有一个明确的递归结束条件。这一点是非常重要的。其实这个出口就是一个条件,当满足了这个条件的时候我们就不再递归了。

三、代码示例:

public class Factorial {

//this is a recursive function

int fact(int n){

if (n==1) return 1;

return fact(n-1)*n;

}

}

public class TestFactorial {

public static void main(String[] args) {

// TODO Auto-generated method stub

Factorial factorial=new Factorial();

System.out.println("factorial(5)="+factorial.fact(5));

}

}

代码执行流程图如下:

此程序中n=5就是程序的出口。

我是学java的,谁能给我说说递归算法是怎么算来着,最好给个例子,给个看得懂的,简单一点的,

递归就是不断的调用其自身,直到满足某一个特定条件之后,才不再调用自身这个方法,有点类似于do...while循环,比如说计算1到10的和,写成一个do...while如Help的doWhile()类方法,写成递归就是先写一个方法,然后在需要的地方,调用这个方法就是了。这里的递归方法是leiJia(),调用是在main里面的leiJia(10)这里,代码如下:

public class Help {

public static void main(String[] args) {

doWhile();

System.out.println("sum = " + leiJia(10));

}

public static void doWhile() {

int i = 1, sum = 0;

do {

sum += i;

} while (i++  10);

System.out.println("sum = " + sum + ", i = " + i);

}

public static int leiJia(int number) {

if (number == 1) {

return 1;

} else {

return number + leiJia(number - 1);

}

}

}

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; i a.length; i++) {

b += a[i];

Combine(a, num - 1, b, i+1, a.length);

b=b.substring(0, b.length()-1);

}

}

}

}


分享标题:java递归算法例子代码 java递归算法经典题目
转载源于:http://lszwz.com/article/dopjohc.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款