Java经典递归算法

Posted gaobing1252

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java经典递归算法相关的知识,希望对你有一定的参考价值。

 

斐波那契数列

package com.luna.base;
public class BirthRabbit 
    public static void main(String[] args) 
        int i = 1;
        for (i = 1; i <= 20; i++) 
            System.out.println("兔子第" + i + "个月的总数为:" + f(i));
        
    
    public static int f(int x) 
        if (x == 1 || x == 2) 
            return 1;
         else 
            return f(x - 1) + f(x - 2);
        
    

从1到100相加

package com.luna.base;
public class Plus 
    public int sum(int i) 
        if (i == 1) 
            return 1;
        
        return i + sum(i - 1);
    
    public static void main(String[] args) 
        Plus plus = new Plus();
        System.out.println("计算结果:" + plus.sum(100) + "!");
    

100的阶乘

package com.luna.base;
import java.math.BigInteger;
public class LoopMutiply 
    public BigInteger sum(int i) 
        if (i == 1) 
            return BigInteger.ONE;
        
        return BigInteger.valueOf(i).multiply(sum(i - 1));
    
    public static void main(String[] args) 
        LoopMutiply test = new LoopMutiply();
        try 
            System.out.println("计算结果:" + test.sum(50) + "!");
         catch (Exception e) 
            e.printStackTrace();
        
    

有序数组a、b合并成一个新的有序数组

package com.luna.base;
public class ArraySort 
    public static void main(String[] args) 
        int[] a =  1, 3, 4 ;
        int[] b =  2, 3, 5, 6 ;
        int[] c = mergeArray(a, b);
        for (int n : c) 
            System.out.print(n + " ");
        
    
    // 合并数组
    public static int[] mergeArray(int[] a, int[] b) 
        int result[] = new int[a.length + b.length];
        if (checkSort(a) && checkSort(b)) 
            // 说明ab数组都是有序的数组
            // 定义两个游标
            int i = 0, j = 0, k = 0;
            while (i < a.length && j < b.length) 
                if (a[i] <= b[j]) 
                    result[k++] = a[i++];
                 else 
                    result[k++] = b[j++];
                
            
            while (i < a.length) 
                // 说明a数组还有剩余
                result[k++] = a[i++];
            
            while (j < b.length) 
                result[k++] = b[j++];
            
        
        return result;
    
    // 检查一个数组是否是有序1 2 3
    public static boolean checkSort(int[] a) 
        boolean flag = false;// 默认不是有序的
        for (int i = 0; i < a.length - 1; i++) 
            if (a[i] > a[i + 1]) 
                // 说明不是有序的
                flag = false;
                break;
             else 
                flag = true;
            
        
        return flag;
    

归并排序算法实现

package com.luna.base;
public class MergingSort     
    public static void sort(int[] data, int left, int right)     
        if (left < right)     
            // 首先找出中间的索引    
            int center = (left + right) / 2;    
    
            // 对中间索引左边的数组进行递归    
            sort(data, left, center);    
    
            // 对中间索引右边的数组进行递归    
            sort(data, center + 1, right);    
            // 合并    
            merge(data, left, center, right);    
            
        
    
    public static void merge(int[] data, int left, int center, int right)     
        int[] tmpArr = new int[data.length];        
        int mid = center + 1;      
        // third记录中间数组的索引    
        int third = left;    
        int tmp = left;        
        while (left <= center && mid <= right)     
            // 将两个数组中取出最小的数放入中间数组    
            if (data[left] <= data[mid])     
                tmpArr[third++] = data[left++];    
             else     
                tmpArr[third++] = data[mid++];    
                
            
    
        // 剩余部分依次放入中间数组    
        while (mid <= right)     
            tmpArr[third++] = data[mid++];    
               
        while (left <= center)     
            tmpArr[third++] = data[left++];    
                        
        while(tmp <= right)    
            data[tmp] = tmpArr[tmp++];    
            
        
    
    public static void main(String[] args)     
        int[] a =  3, 2, 5, 4 ;               
        sort(a, 0, a.length - 1);        
        for (int i = 0; i < a.length; i++)     
            System.out.print(a[i] + " ");    
            
        

九九

public static void main(String[] args) 
for (int i = 1 ; i <= 9 ; i++)
for (int j = 1 ; j <= i ; j++)
System.out.print(i + "*" + j + "=" + i * j + " ");

System.out.println();

 

以上是关于Java经典递归算法的主要内容,如果未能解决你的问题,请参考以下文章

Java经典递归算法

js实现递归算法

求java递归算法,帮我把模块表里面的数据遍历,然后转成json形式传到前台,形成树形事后加分+,非常感谢

java经典算法题——猴子吃桃

算法初步--递归思想(java实现)

算法初步--递归思想(java实现)