JAVA中递归的概念

说简单点就是方法调用自身方法。给你个例子:

猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

public class MonkeyPeach {

public static void main(String[] args) {

System.out.println("第一天的桃子数:"+getPeach_Num(10, 1));

}

// 利用递归的方法来求第一天的桃子数,输入参数为天数和当天的桃子数,输出为第一天桃子数

public static int getPeach_Num(int day, int peach_num) {

if (day == 1)

return peach_num;

else if (day < 1 || peach_num < 0)

return 0;

else

return getPeach_Num(day - 1, (peach_num + 1) * 2);

}

}我这个是递归的方式:public class demo{public void sortarray(int[] array,int m,int n){if(m>0){if(array[n]=m){sortarray(array,m-1,1);}else{sortarray(array,m,n+1);}}}void swap(int[] array,int k){int temp = array[k];array[k] = array[k-1];array[k-1]= temp;}public void showarray(int[]array){for(int i = 0;i

2011-10-13

0

java递归(java递归求阶乘)_java递归(java递归求阶乘)

用Java编程求(递归方法)

1。汉诺塔

这是递归的超经典的例子,几乎每本程序设计书上谈到递归都会介绍。具体情景不再赘述。以我上述的方法观之:(1)递归的出口在于disk数为一的时候

(2)向出口逼近:如果不是一,是n ,则我们先挪动上面n-1块disk,等上面挪完,即递归返回的时候,我们挪动最底下的disk。

仅仅如此,一个貌似十分复杂的问题就解决了,因为挪动那n-1块disk的时候,会继续向上减少,直到disk的数量为一为止。下面给出java程序编码(已测试过,运行正常):

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;

System.out.println(cout);

} else {

for (i = low; i <= high; i++) {

char temp = list[low];

list[low] = list;

list = temp;

permute(list, low + 1, high);

temp = list[low];

list[low] = list;

list = 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;

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

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

}//计算1到n的累加(n不可以为负)

public int countN(int n){

//如果n是1或0返回自己

if(n <= 1){

return n;

//如果n大于1,返回(1到n-1的累加值+n)

return countN(n-1) + n;

}希望对你有帮助

public class factorial {

public static void main(string[] args) {

int temp = recursive(4);

system.out.println(temp);

public static int recursive(int temp){ //递归方法

if(temp==1){//等于1就返回1,否则继续调用返回temp*recursive(temp-1)

return 1;

return temp*recursive(temp-1);

}这不没问题吗?