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.获取文件目录下的所有文件夹和文件

文件目录如图所示:

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);

}

}

}

效果演示:

java 算法递归案例_JAVA 几个递归算法实例相关推荐

  1. java同步方法完成案例_Java同步代码块和同步方法原理与应用案例详解

    本文实例讲述了java同步代码块和同步方法.分享给大家供大家参考,具体如下: 一 点睛 所谓原子性WOmoad:一段代码要么执行,要么不执行,不存在执行一部分被中断的情况.言外之意是这段代码就像原子一 ...

  2. java策略模式案例_java策略模式典型案例

    java策略模式典型案例 java策略模式典型案例 [var1] 参考代码 : https://github.com/zhang-xiaoxiang/DesignPatterns23 没有用策略模式我 ...

  3. java多递归调用_java – 递归调用方法

    我无法绕过递归,更具体地说是我的教科书中提供的语法.它看起来像这样: public int sum (int num) { int result; if (num == 1) result =1; e ...

  4. java自定义事件案例_Java Custom Exception Example(Java自定义异常案例)

    In this example we will look briefly(短暂的) at the basics of Exception, in Java Programming Language. ...

  5. 小红书java算法难吗_Java面试系列之记一次小红书之旅

    一面一面面试官看着二十七八岁,文质彬彬,这哪里是写代码的,头发都飘起来了好么.上来就干项目,由于大家的项目都不太一样,所以对于项目部分我就说说我面试的时候经常遇到的问题描述下项目一口是吃不了胖子的,描 ...

  6. java算法排列式_JAVA 蓝桥杯算法 全排列 背公式即可

    什么是全排列? 所谓全排列就是把几个字符或数字(以下称为元素),进行全部排列 例如:字符串 abc 那么就可以这样排abc acb bac bca cab cba 把全部元素能用到的排列方式进行全部排 ...

  7. Java解决递归栈溢出_Java解决栈溢出

    1,什么是栈溢出? 因为栈一般默认为1-2m,一旦出现死循环或者是大量的递归调用,在不断的压栈过程中,造成栈容量超过1m而导致溢出. 2,解决方案: 方法一:用栈把递归转换成非递归 通常,一个函数在调 ...

  8. java算法提高 扫雷_Java实现 蓝桥杯VIP 算法提高 扫雷

    算法提高 扫雷 时间限制:1.0s 内存限制:256.0MB 问题描述 扫雷游戏你一定玩过吧!现在给你若干个n×m的地雷阵,请你计算出每个矩阵中每个单元格相邻单元格内地雷的个数,每个单元格最多有8个相 ...

  9. java 积累递归深度_java递归的深度

    递归的深度 在使用递归的时候经常会抛出StackOverflowError,顾名思义就是栈满了,而我们这里所说的栈在java中通常就是虚拟机栈(vm stack),在每个方法执行的同时都会创建一个栈帧 ...

  10. java过滤器经典案例_JAVA语言基础的经典案例:猜字母游戏

    设计数据结构 问题 猜字母游戏规则为,随机产生5个按照一定顺序排列的字符作为猜测的结果,由玩家来猜测此字符串,玩家可以猜测多次,每猜测一次,由系统提示结果,如果猜测的完全正确则游戏结束,计算玩家的游戏 ...

最新文章

  1. java绘制地球绕太阳转_Unity3D 公转小案例:地球围绕太阳转
  2. 12亿行代码,阿里巴巴这一年的技术报告和梦想报告
  3. disruptor RingBuffer初始化与生产者事件产生
  4. FaceBoxes的学习笔记
  5. ❤️Spring的声明式事务
  6. 我的开源项目:JPEG分析器
  7. 【危险品识别】基于matlab颜色直方图危险品识别【含Matlab源码 470期】
  8. matlab 支撑集,基于OMP算法的快速压缩感知图像重构
  9. [BUUCTF]REVERSE——相册
  10. 【暗恋不可耻但无用】QQ空间爬虫-Python版(pyzone-crawler)
  11. BIN转HEX,HEX转BIN,互相转换工具,PIC
  12. 学习纹理格式(DXGI_FORMAT 和 VkFormat)
  13. 车联网群雄逐鹿,通信业将如何掘金?
  14. onclick事件诡异事件 一
  15. Horner规则求多项式
  16. 大会员 python
  17. 音频频谱 via FFT
  18. golang go doc 与 godoc 文档生成查看
  19. 长沙市21中2021年高考成绩查询,长沙几大高中名校2020年高考成绩放榜了,这样的成绩你满意吗...
  20. 单片机精确延时几种方式

热门文章

  1. Eclipse(ADT)找不到android.support.v4.view.ViewPager,2步搞定!
  2. 7-10 数组循环左移 (20 分)
  3. 八大排序算法思想介绍
  4. 【Android】AsyncTask机制
  5. 15-struct(构造函数,重载)
  6. 四则运算2之设计思路篇
  7. COJ 1079 树上的查询 (离线LCA)
  8. Sqlite程序及库文件——整理(懒人)
  9. OSPF报文分类与格式
  10. MySQL主从同步(五)——排错思路