java遞歸的應用和實例


使用計算機計算組合數:

1.使用組合數公式利用n!來計算

設計思想

(1)首先解決求n!的函數

(2)再結合組合數公式,求組合數

程序流程圖

 

 

源程序代碼

package Zuote;

import java.math.BigInteger;

import java.util.Scanner;

public class Zuoye1 {

public static void main(String args[])

{

       Scanner input=new Scanner(System.in);

       int n,k;//組合數公式中的n k

       System.out.println("請輸入組合數公式的n和k:");

       n=input.nextInt();

       k=input.nextInt();

       while(k>=n)//判斷輸入是否符合公式,不符合提示錯誤,重新輸入

       {

              System.out.println("輸入錯誤,請重新輸入");

              System.out.println("請輸入組合數公式的n和k:");

              n=input.nextInt();

              k=input.nextInt();

       }

       long C;

       C=calculateN(n)/(calculateN(k)*calculateN(n-k));

       System.out.println("結果為"+C);

}

public static long calculateN(int n)//計算n!的遞歸公式

{

       if(n==1 || n==0){

              return 1;

       }

      

       return n*calculateN(n-1);

}

}

結果截圖

 

 

2.使用遞推的方法用楊輝三角形計算

設計思想

(1)     構建楊輝三角

(2)     組合數結果就相當於楊輝三角中的一個數,n為行,k為列

(3)     輸出相應的數就可以

程序流程圖

 

 

源程序代碼

package Zuote;

import java.util.Scanner;

public class Zuoye2 {

    public static void main(String args[])

    {

      

       Scanner input=new Scanner(System.in);

       int n,k;//組合數公式中的n k

       System.out.println("請輸入組合數公式的n和k:");

       n=input.nextInt();

       k=input.nextInt();

       int [][]f=new int[27][27];//構建楊輝三角

        f[0][0] = 1; 

        for(int i = 1;i <= 24;i++) 

        { 

            for(int j = 1;j <= i + 1;j++) 

            { 

                f[i][j] = f[i - 1][j - 1] + f[i - 1][j]; 

            } 

        } 

        System.out.println("組合結果為"+f[n+1][k+1]);//輸出結果

    }

}

結果截圖

 

 

3. 使用遞歸的方法用組合數遞推公式計算

設計思想

(1)    遞歸組合數,相當於楊輝三角

(2)     構建遞歸函數,n,k相應於楊輝三角的行列

程序流程圖

 

 

源程序代碼

package Zuote;

 

import java.util.Scanner;

 

public class Zuoye3 {

    public static void main(String args[])

    {

       Scanner input=new Scanner(System.in);

       int n,k;//組合數公式中的n k

       System.out.println("請輸入組合數公式的n和k:");

       n=input.nextInt();

       k=input.nextInt();

        System.out.println("組合結果為"+ZuHe(n,k));

    }

    public static long ZuHe(int a,int b) //構造遞歸函數

    {

       if(b==0)return 1;

       else {

           if(a==1)return 1;

              else {

                      if(a==b)return 1;

                           else {

                                    return (ZuHe(a-1,b-1)+ZuHe(a-1,b));

                                }

                      }

           }

    }

}

結果截圖

 

 

4.遞歸編程解決漢諾塔問題。用Java實現

設計思想

(1)    遞歸移動n個圓盤,先移動n-1個,再把第n個移動到c上

(2)    遞歸,依次移動

程序流程圖

 

 

源程序代碼

package Zuote;

 

import java.util.Scanner;

 

public class Zuoye4 {

    public static void main(String args[])

    {

       int n;//有n個圓盤

       char A='A';

       char B='B';

       char C='C';

       Scanner input=new Scanner(System.in);

       System.out.println("請輸入n:");

       n=input.nextInt();

       Hanoi( n,A,B,C);

    }

    public static void Hanoi(int n,char A,char B,char C)//將 n個圓盤從A移動到C,B做輔助

    {

       if(n==1)

       {

           move(A,1,C);

       }

       else

       {

           Hanoi(n-1,A,C,B);

           move(A,n,C);

           Hanoi(n-1,B,A,C);

       }

    }

    public static void move(char A,int n,char B)//將第n個圓盤從A移動到B

    {

        System.out.println("第"+n+"個圓盤從"+A+"->"+B);

    }

}

結果截圖

 

 

5.使用遞歸方式判斷某個字串是否是回文

設計思想

(1)    從開頭開始依次比較首尾字符

(2)    如果相同,再比較第二個字符和倒數第二個

(3)    依次比較直到最后完全相同,輸出

程序流程圖

 

 

源程序代碼

package Zuote;

import java.util.Scanner;

public class Zuoye5 {

    public static void main(String args[])

    {

       Scanner input=new Scanner(System.in);

       String s=input.next();//輸入字符串

       int i,j;

       i=0;//為字符串第一個字符

       j=s.length()-1;//最后一個字符

       System.out.println("是否是回文:"+HuiWen(s,i,j));

    }

    public static boolean HuiWen(String s,int i,int j)//判斷字符串是否回文

    {

       if(i==j)

       {

           return true;

       }

       else

           if((i-1)==j)

           {

              return true;

           }

       return (s.charAt(i)==s.charAt(j))&&HuiWen(s,i+1,j-1);

    }

}

結果截圖

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM