java 算法递归案例_JAVA 几个递归算法实例

1、求1-n的和

public static int getSum(intn) {if(n == 1)return 1;return n+getSum(n-1);

}public static voidmain(String[] args){

System.out.println(getSum(100));//5050

}

2、输出斐波那契数列

public static int getFibonacci(intn) {if(n==1)return 1;if(n==2)return 1;return getFibonacci(n-1)+getFibonacci(n-2);

}public static voidmain(String[] args){for(int i = 1;i<=10;i++) {

System.out.print(getFibonacci(i)+" ");

}

//1 1 2 3 5 8 13 21 34 55

}

3、遍历二叉树(让用户输入数据,并排序输出)

classNode {intvalue;

Node left;

Node right;

Node(intinput){

value=input;

}

}public classMain {public static void put(Node node,intnumber) {if(number<=node.value) {if(node.left == null) {

node.left= newNode(number);

}else{

put(node.left,number);

}

}else{if(node.right == null) {

node.right= newNode(number);

}else{

put(node.right,number);

}

}

}public static voidshowasc(Node node) {if(node.left!=null) {

showasc(node.left);

}

System.out.print(node.value+" ");if(node.right!=null) {

showasc(node.right);

}

}public static voidshowdesc(Node node) {if(node.right!=null) {

showdesc(node.right);

}

System.out.print(node.value+" ");if(node.left!=null) {

showdesc(node.left);

}

}public static voidmain(String[] args){

Scanner sc= newScanner(System.in);

System.out.println("您希望存几个数?");int count =sc.nextInt();

System.out.println("请输入第1个数:");

Node root= newNode(sc.nextInt());for(int i = 2;i<=count;i++) {

System.out.println("请输入第"+i+"个数:");int number =sc.nextInt();

put(root,number);

}

System.out.println("从小到大排序为:");

showasc(root);

System.out.println("\n从大到小排序为:");

showdesc(root);

}

}

运行结果:

您希望存几个数?

6

请输入第1个数:

6

请输入第2个数:

1

请输入第3个数:

5

请输入第4个数:

2

请输入第5个数:

4

请输入第6个数:

3

从小到大排序为:

1 2 3 4 5 6

从大到小排序为:

6 5 4 3 2 1

4.获取文件目录下的所有文件夹和文件

文件目录如图所示:

4654db7aebaf2a7fe8036f6e5201b91b.png

public static voidshow(File f) {for(File fs:f.listFiles()) {if(fs.isFile()) {

System.out.println(fs);

}if(fs.isDirectory()) {

show(fs);

}

}

}public static voidmain(String[] args){

File f= new File("D:\\Recursion");

show(f);

}

运行结果:

D:\Recursion\Documents\Document1.pdf

D:\Recursion\Documents\Document2.ppt

D:\Recursion\Documents\Document3.doc

D:\Recursion\Photos\Photo1.jpg

D:\Recursion\Photos\Photo2.bmp

D:\Recursion\ReadMe.txt

D:\Recursion\Videos\Video1.mp4

D:\Recursion\Videos\Video2.avi

5.汉诺艾塔解法演示

Hanoi类:

importjava.util.Stack;public classHanoi {int layers = 3;//代表汉诺埃塔的三个槽

Stack A = new Stack<>();

Stack B = new Stack<>();

Stack C = new Stack<>();//定义展示矩阵

int[][] hanoi_matrix = null;

Hanoi(intlayers){//高度不能小于1

if(layers<1)this.layers = 1;else

this.layers =layers;//初始化展示矩阵

hanoi_matrix = new int[layers][3];for(int[] row :hanoi_matrix) {for(intcol:row) {

col= 0;

}

}//将n个盘插入a柱中

for(int i =0;i

A.push(layers-i);

}

}//打印hanoi塔

public voidshowHanoi() {//将展示矩阵清空

for(int i = 0;i

hanoi_matrix[i][j]= 0;

}

}//将目前各柱的盘情况映射到展示矩阵上

for(int i = 0;i

hanoi_matrix[layers-i-1][0]=A.get(i);

}for(int i = 0;i

hanoi_matrix[layers-i-1][1]=B.get(i);

}for(int i = 0;i

hanoi_matrix[layers-i-1][2]=C.get(i);

}//按照最合适大小打印hanoi塔

for(int[] row :hanoi_matrix) {for(intradius:row) {for(int i = 0;i

System.out.print(" ");

}for(int i = 0;i

System.out.print("=");

}

System.out.print("|");for(int i = 0;i

System.out.print("=");

}for(int i = 0;i

System.out.print(" ");

}

System.out.print(" ");

}

System.out.println();

}

}//将某柱上最上面一个盘移动到另一个柱上

public void movetop(char from,charto) {//停顿一下便于观察

try{

Thread.sleep(1500);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

Main.count++;

System.out.print("第"+Main.count+"次移动:");

System.out.println("将"+from +"柱顶端的盘移动到"+to+"柱上:");//将源柱弹栈

int d = 0;switch(from) {case 'a':

d=A.pop();break;case 'b':

d=B.pop();break;case 'c':

d=C.pop();break;

}//目标柱压栈

switch(to) {case 'a':

A.push(d);break;case 'b':

B.push(d);break;case 'c':

C.push(d);break;

}

showHanoi();

}

}

测试类:

importjava.util.Scanner;public classMain {public static voidmain(String[] args) {

System.out.println("请输入您希望演示的层数:");int layers = newScanner(System.in).nextInt();

Hanoi h= newHanoi(layers);

System.out.println("原始:");

h.showHanoi();

move(h,layers,'a','c','b');

System.out.println("移动完成!");

}static int count = 0;//移动 传入Hanoi塔对象,想要要移动的层数,源柱,目标柱,辅助柱

public static void move(Hanoi h,int layers,char from,char to,charby) {if(layers == 1) {//如果塔只有一层,直接移过去即可

h.movetop(from, to);

}else{//如果大于一层,最大层上面的n-1层看成一个整体,先移到辅助柱

move(h,layers-1,from,by,to);//将最大的盘移到目标柱

h.movetop(from, to);//将辅助柱上的盘移到目标柱上

move(h,layers-1,by,to,from);

}

}

}

效果演示:

ef7dc7fbf6f449cd79f74ea41b6a7dbe.gif

b189290376b5ba222da8bc9c71247ef7.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值