递归基础练习-1

1、求和

 1 package com.wangymd.recursive;
 2 
 3 /**
 4  * @desc 数组递归求和
 5  * @author wangymd
 6  * @data 2022-07-03 15:35:13
 7  */
 8 public class SumTest {
 9 
10     public static void main(String[] args) {
11         int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8};
12         System.out.println(sum(array1, 0));
13     }
14 
15     public static int sum(int[] array, int index) {
16         if (index == array.length) {
17             return 0;
18         } else {
19             return array[index] + sum(array, index + 1);
20         }
21     }
22 }

 

 

2、数组是否排序判断

 1 package com.wangymd.recursive;
 2 
 3 /**
 4  * @desc 数组是否有序递归
 5  * @author wangymd
 6  * @data 2022-07-03 15:33:55
 7  */
 8 public class ArrayIsSortedTest {
 9 
10     public static void main(String[] args) {
11         int[] array1 = { 1, 2, 3, 4, 5 };
12         System.out.println(isSorted(array1, array1.length, "asc"));
13         int[] array2 = { 1, 2, 3, 6, 5 };
14         System.out.println(isSorted(array2, array2.length, "asc"));
15         
16         int[] array3 = { 5, 4, 3, 2, 1 };
17         System.out.println(isSorted(array3, array3.length, "desc"));
18         int[] array4 = { 5, 4, 3, 4, 1 };
19         System.out.println(isSorted(array4, array4.length, "desc"));
20     }
21 
22     public static boolean isSorted(int[] array, int index, String type) {
23         if (index == 1) {
24             return true;
25         }
26         
27         if("desc".equalsIgnoreCase(type)){
28             return array[index - 1] >= array[index - 2] ? false : isSorted(array, index - 1, type);
29         }else if("asc".equalsIgnoreCase(type)){
30             return array[index - 1] <= array[index - 2] ? false : isSorted(array, index - 1, type);
31         }else{
32             return false;
33         }
34     }
35 }

 

 

3、阶乘

 1 package com.wangymd.recursive;
 2 
 3 /**
 4  * @desc 递归
 5  * @author wangymd
 6  * @data 2022-07-03 15:32:53
 7  */
 8 public class FactorialTest {
 9 
10     public static void main(String[] args) {
11         System.out.println(factorial(6));
12     }
13     
14     /**
15      *     递归阶乘
16      * @param n
17      * @return
18      */
19     public static int factorial(int n) {
20         if(n == 0) {
21             return 1;
22         }else {
23             return n * factorial(n - 1);
24         }
25     }
26 }

 

 

4、汉诺塔

 1 package com.wangymd.recursive;
 2 
 3 /**
 4  * @desc 递归 https://blog.csdn.net/question_mark/article/details/100907008
 5  * @author wangymd
 6  * @data 2022-07-03 15:35:37
 7  */
 8 public class HanoiTest {
 9 
10     public void hanoi(int n, char A, char B, char C) {
11         if (n == 1) {
12             move(A, C);//最大的圆盘从A移动到C
13             return;
14         }
15         hanoi(n - 1, A, C, B);//C为辅助
16         move(A, C);
17         hanoi(n - 1, B, A, C);//A为辅助
18     }
19 
20     private void move(char A, char C) {
21         System.out.println("move: " + A + "----->" + C);
22     }
23 
24     public static void main(String[] args) {
25         HanoiTest hanoiTest = new HanoiTest();
26         hanoiTest.hanoi(4, 'A', 'B', 'C');//移动A到C
27     }
28 }

 

 1 package com.wangymd.recursive;
 2 
 3 import java.util.Stack;
 4 
 5 /**
 6  * @desc 递归 https://blog.csdn.net/question_mark/article/details/100907008
 7  * @author wangymd
 8  * @data 2022-07-03 15:36:32
 9  */
10 public class HanoiTest2 {
11     
12     public static Stack<Integer> hanoiA = new Stack<Integer>();
13     public static Stack<Integer> hanoiB = new Stack<Integer>();
14     public static Stack<Integer> hanoiC = new Stack<Integer>();
15     
16     public static Integer times = 0;
17 
18     public void hanoi(int n, Stack<Integer> A, Stack<Integer> B, Stack<Integer> C) {
19         if (n == 1) {
20             move(A, C);//最大的圆盘从A移动到C
21             return;
22         }
23         hanoi(n - 1, A, C, B);//C为辅助,n-1移动到B
24         move(A, C);//A最下面的移动到C
25         hanoi(n - 1, B, A, C);//A为辅助,B上n-1个移动到C
26     }
27 
28     private void move(Stack<Integer> A, Stack<Integer> C) {
29         System.out.println("<<<<<<<<第" + (++times) + "移动>>>>>>>>");
30         C.push(A.pop());
31         System.out.println("移动后hanoiA:" + hanoiA + " ,hanoiB:" + hanoiB + " ,hanoiC:" + hanoiC);
32     }
33 
34     public static void main(String[] args) {
35         HanoiTest2 hanoiTest = new HanoiTest2();
36         
37         hanoiA.push(1);
38         hanoiA.push(2);
39         hanoiA.push(3);
40         hanoiA.push(4);
41         hanoiA.push(5);
42         hanoiA.push(6);
43         
44         System.out.println("移动前hanoiA:" + hanoiA);
45         hanoiTest.hanoi(hanoiA.size(), hanoiA, hanoiB, hanoiC);//移动A到C
46         System.out.println("移动完成hanoiC:" + hanoiC);
47     }
48 }

 

posted @ 2022-07-03 15:57  心动如雷  阅读(23)  评论(0编辑  收藏  举报