1、求1-n的和
public static int getSum(int n) { if(n == 1) return 1; return n+getSum(n-1); } public static void main(String[] args){ System.out.println(getSum(100)); //5050 }
2、输出斐波那契数列
public static int getFibonacci(int n) { if(n==1) return 1; if(n==2) return 1; return getFibonacci(n-1)+getFibonacci(n-2); } public static void main(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、遍历二叉树(让用户输入数据,并排序输出)
class Node { int value; Node left; Node right; Node(int input){ value = input; } } public class Main { public static void put(Node node,int number) { if(number<=node.value) { if(node.left == null) { node.left = new Node(number); }else { put(node.left,number); } }else { if(node.right == null) { node.right = new Node(number); }else { put(node.right,number); } } } public static void showasc(Node node) { if(node.left!=null) { showasc(node.left); } System.out.print(node.value+" "); if(node.right!=null) { showasc(node.right); } } public static void showdesc(Node node) { if(node.right!=null) { showdesc(node.right); } System.out.print(node.value+" "); if(node.left!=null) { showdesc(node.left); } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("您希望存几个数?"); int count = sc.nextInt(); System.out.println("请输入第1个数:"); Node root = new Node(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 void show(File f) { for(File fs:f.listFiles()) { if(fs.isFile()) { System.out.println(fs); } if(fs.isDirectory()) { show(fs); } } } public static void main(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类:
import java.util.Stack; public class Hanoi { int layers = 3; //代表汉诺埃塔的三个槽 Stack<Integer> A = new Stack<>(); Stack<Integer> B = new Stack<>(); Stack<Integer> C = new Stack<>(); //定义展示矩阵 int[][] hanoi_matrix = null; Hanoi(int layers){ //高度不能小于1 if(layers<1) this.layers = 1; else this.layers = layers; //初始化展示矩阵 hanoi_matrix = new int[layers][3]; for(int[] row :hanoi_matrix) { for(int col:row) { col = 0; } } //将n个盘插入a柱中 for(int i =0;i<layers;i++) { A.push(layers-i); } } //打印hanoi塔 public void showHanoi() { //将展示矩阵清空 for(int i = 0;i<hanoi_matrix.length;i++) { for(int j = 0;j<hanoi_matrix[i].length;j++) { hanoi_matrix[i][j] = 0; } } //将目前各柱的盘情况映射到展示矩阵上 for(int i = 0;i<A.size();i++) { hanoi_matrix[layers-i-1][0]=A.get(i); } for(int i = 0;i<B.size();i++) { hanoi_matrix[layers-i-1][1]=B.get(i); } for(int i = 0;i<C.size();i++) { hanoi_matrix[layers-i-1][2]=C.get(i); } //按照最合适大小打印hanoi塔 for(int[] row :hanoi_matrix) { for(int radius:row) { for(int i = 0;i<(layers-radius);i++) { System.out.print(" "); } for(int i = 0;i<radius;i++) { System.out.print("="); } System.out.print("|"); for(int i = 0;i<radius;i++) { System.out.print("="); } for(int i = 0;i<(layers-radius);i++) { System.out.print(" "); } System.out.print(" "); } System.out.println(); } } //将某柱上最上面一个盘移动到另一个柱上 public void movetop(char from,char to) { //停顿一下便于观察 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(); } }
测试类:
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("请输入您希望演示的层数:"); int layers = new Scanner(System.in).nextInt(); Hanoi h = new Hanoi(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,char by) { 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); } } }
效果演示: