递归

mac2023-01-24  19

阶乘

public class Factorial { public static void main(String[] args) { System.out.print("请输入你想要计算的数字:"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); long result = factorial(num); System.out.println(num + "的阶乘是:" + result); } public static long factorial(int num) { if(num == 0){ return 1; } else { return num * factorial(num - 1); } } }

斐波那契数列

public class ComputeFibonacci { public static void main(String[] args) { System.out.print("请输入你想要计算的数字:"); Scanner sc = new Scanner(System.in); int index = sc.nextInt(); long result = fibonacci(index); System.out.println("最终结果是:" + result); } public static long fibonacci(long index) { if(index == 0) {//基础情形 return 0; }else if (index == 1){//基础情形 return 1; }else { return fibonacci(index-1) + fibonacci(index-2); } } }

判断回文串

/** * 利用重载方法先把传入的字符串地位置准备好,然后再一一比较 * 第一个方法是判断字符串s是不是回文串 * 第二个方法是判断s(low ... high)是不是回文串 * 在递归程序设计定义中定义第二个方法来接收附加的参数是一个常用的设计技巧,这样的方法被称为递归辅助方法 * 递归辅助方法在设计关于字符串和数组的解决方案上是非常有用的。 * @author Lenovo */ public class RecursivePalindrome { public static void main(String[] args) { System.out.println("Is moon a palindrome?" + "\t" + isPalindrome("moon")); System.out.println("Is noon a palindrome?" + "\t" + isPalindrome("noon")); System.out.println("Is a a palindrome?"+ "\t" + isPalindrome("a")); System.out.println("Is aba a palindrome?" + "\t" + isPalindrome("aba")); System.out.println("Is ab a palindrome?" + "\t" + isPalindrome("ab")); } public static boolean isPalindrome(String s) { return isPalindrome(s, 0, s.length()-1); } public static boolean isPalindrome(String s, int low, int high) { if(high <= low) { //基础情形0或1位 return true; }else if (s.charAt(0) == s.charAt(s.length() - 1)) { return false; }else { return isPalindrome(s, low+1, high-1); } } }

递归二分查找

/** * 递归二分查找 * 前提是数组已经排好序了 * 先从中间开始查找 * 若小于中间的数据,就从前面开始递归查找 * 若大于中间的数据,就从后面开始递归查找 * 若刚好等于中间的数据,则结束查找 * 缺点就是有重复的话,不一定找到哪个 * @author Lenovo * */ public class RecursiveBinarySearch { public static void main(String[] args) { int[] list1 = {1,1,2,7,9,3,4,5,6,8}; Arrays.sort(list1); for (int i : list1) { System.out.println(i); } System.out.println(recursiveBinarySearch(list1, 1)); } public static int recursiveBinarySearch(int[] list, int key) { int low = 0; int high = list.length-1; return recursiveBinarySearch(list, key, low, high); } private static int recursiveBinarySearch(int[] list, int key ,int low, int high) { if(low > high) { return -low-1; } int mid = (low + high) / 2; if(key < list[mid]) { return recursiveBinarySearch(list, key, low, mid-1); }else if(key == list[mid]){ return mid; }else { return recursiveBinarySearch(list, key, mid+1, high); } } }

递归选择排序

/** * 递归选择排序sort的底层基本实现 * 选择排序法是找到列表中最小的数,再将它与第一个元素交换,从小到大排序 * 说到底,递归辅助方法就是把拿来的数据先规划一下,把需要的数据从中提取出来 * 然后把它传给其他人 * @author Lenovo * */ public class RecursiveSelectionSort { public static void main(String[] args) { double[] list = {1, 3,5,4,2,6,5,9,7,8}; sort(list); for (double i : list) { System.out.print(i+"\t"); } } public static void sort(double[] list) { sort(list, 0, list.length -1); } private static void sort(double[] list, int low, int high) { if(low < high) { int indexOfMin= low; double min = list[low]; for (int i = low+1; i <= high; i++) { if(list[i] < min) { min = list[i]; indexOfMin = i; } } list[indexOfMin] = list[low]; list[low] = min; sort(list, low+1, high); } } }

汉诺塔

/** * 汉诺塔问题,用递归方法解决方式 * @author Lenovo * */ public class TowerOfHanoi { public static void main(String[] args) { moveDisks(3, 'A','B','C'); } public static void moveDisks(int n, char fromTower, char toTower, char auxTower) { if (n == 1) { System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); }else { moveDisks(n-1, fromTower, auxTower, toTower); System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); moveDisks(n-1, auxTower, toTower, fromTower); } } }
最新回复(0)