资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

递归的java代码 java中的递归

java递归算法

1.汉诺塔问题

坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都垃圾桶小微创业公司专业提供企业网站制作营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。

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递归查询子节点,按给的示例代码实现

代码如下:

import java.util.ArrayList;

import java.util.List;

class Org {

private String id;

private String name;

private String pid;

public Org(String id, String name, String pid) {

this.id = id;

this.name = name;

this.pid = pid;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPid() {

return pid;

}

public void setPid(String pid) {

this.pid = pid;

}

@Override

public String toString() {

return "Org [id=" + id + ", name=" + name + ", pid=" + pid + "]";

}

}

public class App {

static void find(ListOrg list, String pid) {

list.stream().filter(p - p.getPid().equals(pid))

.forEach(org - {

System.out.println(org);

find(list, org.getId());

});

}

public static void main(String[] args) {

ListOrg list = new ArrayList();

list.add(new Org("111", "公司", "0"));

list.add(new Org("222", "部门", "111"));

list.add(new Org("333", "小组", "222"));

list.add(new Org("444", "员工1", "333"));

list.add(new Org("555", "员工2", "333"));

find(list, "0");

System.out.println("------------------------------------");

find(list, "111");

}

}

运行结果:

一段JAVA的递归代码

下面递归写了一段递归累加到100,每加20个就换行输出。

package zhidao;

public class Digui {

public static int add(int num){

int sum = 0;

StringBuffer sb = new StringBuffer();

if (num = 0) {

return 0;

}else{

if (num == 1) {

sum = sum+1;

}else {

sum = add(num-1)+num;

}

if (num % 20 == 0) {

System.out.println("[index = "+num+" sum = "+sum+"]");

}else {

System.out.print("[index = "+num+" sum = "+sum+"],");

}

}

return sum;

}

public static void main(String[] args) {

add(100);

}

}

用java递归方法实现

1、递归做为一种算法在程序设计语言中广泛使用,是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象。

2、递归算法一般用于解决三类问题:

1)数据的定义是按递归定义的。(Fibonacci(斐波那契)的函数)

2)问题解法按递归算法实现。(回溯)

3)数据的结构形式是按递归定义的。(树的遍历,图的搜索)


文章题目:递归的java代码 java中的递归
文章起源:http://cdkjz.cn/article/hgigsp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220