• java : 递归实例


    当原方法的实现和子方法相同,并且子方法进一步简化了原方法(执行步骤减少了,判断的次数减少了等),可以考虑使用递归处理问题

    1.使用递归实现斐波那契数列

    View Code
     1 //使用递归实现斐波那契数列
     2 import java.util.* ;
     3 public class TestFibonacci
     4 {
     5    public static void main(String[] args)
     6    {
     7        Scanner input = new Scanner(System.in) ;
     8        int count = 0 ;
     9        int num ;
    10        System.out.print("请输入斐波那契数列的个数: ") ;
    11        num = input.nextInt() ;
    12        for(int i = 1 ; i<=num ;i++)
    13        {
    14            System.out.print(fibonacci(i)+" ") ;
    15            count++ ;
    16            if(count%10==0)
    17               System.out.println() ;
    18         }
    19     }
    20    public static int fibonacci(int num)
    21    {
    22        if(num==1||num==2)
    23            return 1 ;
    24        else
    25            return (fibonacci(num-2)+fibonacci(num-1)) ;
    26    }
    27 }

    2.使用递归实现阶乘

    View Code
     1 //使用递归实现阶乘
     2 import java.util.* ;
     3 public class TestFactorial
     4 {
     5    public static void main(String[] args)
     6    {
     7        Scanner input = new Scanner(System.in) ;
     8        int n ;
     9        System.out.print("请输入阶乘n!中的n :") ;
    10        n = input.nextInt() ;
    11        int result = factorial(n) ;
    12        System.out.println("The result of "+n+"! is "+result) ;
    13     }
    14     public static int factorial(int n)
    15     {
    16         if(n==1)
    17            return 1 ;
    18         else
    19            return n*factorial(n-1) ;
    20     }
    21 }

    3.使用递归打印N次消息

    View Code
     1 //使用递归打印N次消息
     2 public class TestRecursionOutString
     3 {
     4     public static void main(String[] args)
     5     {
     6         int time = 10 ;
     7         nPrint("I love you.",time) ;
     8     }
     9     public static void nPrint(String message,int time)
    10     {
    11         if(time>=1)
    12            System.out.println(message) ;
    13         if(time-1>0)
    14            nPrint(message,time-1) ;
    15     }
    16 }

    4.使用递归实现回文判断

    View Code
     1 //使用递归实现回文判断
     2 import java.util.* ;
     3 public class TestIsPalindrome
     4 {
     5    public static void main(String[] args)
     6    {
     7        while(true)
     8        {
     9           Scanner input = new Scanner(System.in) ;
    10           System.out.print("请输入一个字符串(判断是否为回文符,输入'end'时,推出程序): ") ;
    11           String palindrome = input.next() ;
    12           if(palindrome.equals("end")==true)
    13                break ;
    14           else
    15              System.out.println(palindrome+" is a palindrome ? "+isPalindrome(palindrome)) ;
    16         }
    17     }
    18    public static boolean isPalindrome(String palindrome)
    19    {
    20        if(palindrome.length()==1)
    21          return true ;
    22        else
    23           if(palindrome.length()==2)
    24           {
    25               if(palindrome.charAt(0)==palindrome.charAt(palindrome.length()-1))
    26                  return true ;
    27               else
    28                  return false ;
    29           }
    30           else
    31              if(palindrome.charAt(0)!=palindrome.charAt(palindrome.length()-1))
    32                 return false ;
    33              else
    34                 return isPalindrome(palindrome.substring(1,palindrome.length()-1)) ;
    35     }
    36 }

    递归辅助方法

    5.通过递归辅助方法重写回文符的判断

    View Code
     1 //通过递归辅助方法重写回文符的判断
     2 import java.util.* ;
     3 public class TestIsPalindrome2
     4 {
     5      public static void main(String[] args)
     6    {
     7        while(true)
     8        {
     9           Scanner input = new Scanner(System.in) ;
    10           System.out.print("请输入一个字符串(判断是否为回文符,输入'end'时,推出程序): ") ;
    11           String palindrome = input.next() ;
    12           if(palindrome.equals("end")==true)
    13                break ;
    14           else
    15              System.out.println(palindrome+" is a palindrome ? "+isPalindrome2(palindrome)) ;
    16         }
    17     }
    18     public static boolean isPalindrome2(String s)
    19     {
    20         return isPalindrome2(s,0,s.length()-1) ;
    21     }
    22     public static boolean isPalindrome2(String s,int low,int high)
    23     {
    24         if(high<=low)
    25            return true ;
    26         else
    27            if(s.charAt(low)!=s.charAt(high))
    28               return false ;
    29            else
    30               return isPalindrome2(s,low+1,high-1) ;
    31     }
    32 }

    6.通过递归实现选择排序

    View Code
     1 //通过递归实现选择排序
     2 public class TestSort
     3 {
     4     public static void main(String[] args)
     5     {
     6         int[] nums = {2,6,7,3,9,5} ;
     7         display(nums) ;
     8         sort(nums) ;
     9         display(nums) ;
    10     }
    11     public static void sort(int[] nums)
    12     {
    13         sort(nums,nums.length-1) ;
    14     }
    15     public static void sort(int[] nums,int high)
    16     {
    17         if(high>=1)
    18         {
    19             int maxIndex = 0 ;
    20             int max = nums[0] ;
    21             for(int i=1 ; i<=high ; i++)
    22             {
    23                 if(max < nums[i])
    24                 {
    25                     maxIndex = i ;
    26                     max = nums[i] ;
    27                 }
    28             }
    29             nums[maxIndex] = nums[high] ;
    30             nums[high] = max ;
    31             sort(nums,high-1) ;
    32         }
    33     }
    34     public static void display(int[] nums)
    35     {
    36         for(int i=0 ;i<nums.length ; i++)
    37            System.out.print(nums[i]+" ") ;
    38         System.out.println() ;
    39     }
    40 }

    7.使用递归实现二分查找

    View Code
     1 //使用递归实现二分查找
     2 public class TestBinarySearch
     3 {
     4    public static void main(String[] args)
     5    {
     6        int[] nums = {2,4,6,8,10,12,14,16} ;
     7        display(nums) ;
     8        int key = 8 ;
     9        int searchNum = binarySearch(nums,key) ;
    10        System.out.println(key+" 所在的下标为: "+searchNum);
    11     }
    12    public static int binarySearch(int[] nums,int key)
    13    {
    14        int low = 0 ;
    15        int high = nums.length-1 ;
    16        return binarySearch(nums,key,low,high) ;
    17     }
    18    public static int binarySearch(int[] nums,int key,int low,int high)
    19    {   
    20        if(low>high)
    21           return -low-1 ;
    22        int mid = (low+high)/2 ;
    23        if(nums[mid]==key)
    24           return mid ;
    25        else 
    26           if(nums[mid]>key)
    27              return binarySearch(nums,key,low,mid-1) ;
    28           else
    29              return binarySearch(nums,key,mid+1,high) ;
    30    }
    31    public static void display(int[] nums)
    32     {
    33         for(int i=0 ;i<nums.length ; i++)
    34            System.out.print(nums[i]+" ") ;
    35         System.out.println() ;
    36     }
    37 }

    8.用递归解决汉诺塔问题

    View Code
     1 //用递归解决汉诺塔问题
     2 public class TowerOfHanoi
     3 {
     4    public static void main(String[] args)
     5    {
     6       int n = 3 ;
     7       System.out.println("The result is ") ;
     8       moveDisks(n,'A','B','C') ;//把n个盘从借助C盘,从A盘搬到B盘
     9     }
    10    public static void moveDisks(int n,char from,char to,char help)
    11    {
    12        if(n==1)
    13           System.out.println("Move disk "+n+" from "+from+" to "+to) ;
    14        else
    15        {
    16            moveDisks(n-1,from,help,to) ;//将n-1个盘借助B盘,从A盘搬到C盘
    17            System.out.println("Move disk "+n+" from "+from+" to "+to) ;//将N盘从A盘搬到B盘
    18            moveDisks(n-1,help,to,from) ;//将n-1个盘借助A盘,从C盘搬到B盘
    19        }
    20     }
    21 }

    9.用递归求得两个整数的最大公约数

    View Code
     1 //求连个整数的最大公约数(使用递归)
     2 //使用辗转相除法:
     3 import java.io.* ; 
     4 public class ComDivisor
     5 {
     6     public static void main(String[] agrs) throws IOException
     7     {
     8         System.out.print("请输入第一个整数: ") ;
     9         int a = getInt() ;
    10         System.out.print("请输入第二个整数: ") ;
    11         int b = getInt() ;
    12         int result = getComDivisor(a,b) ;
    13         System.out.println(a+","+b+"最大的公约数是:"+result) ;
    14     }
    15 //------------------------------------------
    16     public static int getComDivisor(int a,int b)//返回最大公约数
    17     {
    18         if(a<b)
    19            return doComDivisor(a,b) ;
    20         else
    21            return doComDivisor(b,a) ;
    22     }
    23     public static int doComDivisor(int a,int b)//小数在前,大数在后
    24     {
    25         if(b%a==0)
    26            return a ;
    27         else
    28         {
    29             int c = b%a ;
    30             return doComDivisor(c,a) ;
    31         }
    32     }
    33 //------------------------------------------
    34     public static String getString() throws IOException
    35    { 
    36       InputStreamReader is = new InputStreamReader(System.in) ;
    37       BufferedReader br = new BufferedReader(is) ;
    38       String s = br.readLine() ;
    39       return s ;
    40     }
    41 //------------------------------------------
    42     public static int getInt() throws IOException
    43    { 
    44       String s = getString() ;
    45       return Integer.parseInt(s) ;
    46     }
    47 }

     

     

     

     

     

     

     

  • 相关阅读:
    第14周周总结
    第13周周总结
    栈与队列
    前端继承方式汇总
    实现前端资源增量式更新的一种思路
    link和@import引入外部样式的区别
    webSocket
    Java 下载JDK账号
    穿透
    vue.set方法的使用
  • 原文地址:https://www.cnblogs.com/KeenLeung/p/2703503.html
Copyright © 2020-2023  润新知