1. 程式人生 > >02-方法 ——課程作業01-遞歸練習

02-方法 ——課程作業01-遞歸練習

log -方法 angle ring 技術分享 字符 漢諾塔問題 mov .com

1.使用計算機計算組合數

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

程序設計思想:

利用遞歸定義一個方法jiecheng(int n)用來求一個數n的階乘,當n>1時,返回n*jiecheng(n-1),直到n=1時,返回1。輸入底數m和階數n之後,利用該函數分別求出m和n還有m-n的階乘,利用公式m!/n!*(m-n)!在main函數中輸出結果。

程序流程圖:

技術分享

程序源代碼:

 1 import java.util.Scanner;
 2 
 3 public class ZuHeShu {
 4     
 5     public static void main(String[] args) {
6 7 System.out.print("請輸入組合數中的底數m:"); 8 9 Scanner input = new Scanner(System.in); 10 11 int import_m = input.nextInt(); 12 13 System.out.print("請輸入組合數中的階數n:"); 14 15 int import_n = input.nextInt(); 16 17
System.out.println("組合數的結果為:"+jiecheng(import_m)/(jiecheng(import_n)*jiecheng(import_m-import_n))); 18 19 } 20 21 public static long jiecheng(int i) 22 23 { 24 25 if(i > 1) 26 27 { 28 29 return i * jiecheng(i - 1);
30 31 } 32 33 else 34 35 { 36 37 return 1; 38 39 } 40 41 } 42 43 }

程序測試截圖:

技術分享

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

程序設計思想:

定義一個二維數組T存儲楊輝三角形,循環賦值構造楊輝三角形:T[i][0] = 1,T[i][i] = 1,(i = 0,1,2……n),其余每個T[i][j] =T[i - 1][j - 1] + T[i - 1][j]。則C(m,n)=T[n][k]。

程序流程圖:

技術分享

程序源代碼:

 1 import java.util.Scanner;
 2 
 3 public class ZuHeShu3 {
 4     
 5      public static void main(String[] args) {
 6          
 7          System.out.print("請輸入組合數中的底數m:");
 8          
 9          Scanner input = new Scanner(System.in);
10          
11          int m = input.nextInt();
12          
13          System.out.print("請輸入組合數中的階數n:");
14          
15          int n= input.nextInt();
16          
17          System.out.println("組合數的結果為:"+T(m,n));
18          
19      }
20      
21      public static int T(int m,int n){
22          
23          int[][] Y = new int[m + 1][m + 1];
24          
25          Y[0][0] = 1;
26          
27          for(int i = 0;i < m + 1;i++)
28              
29          {
30             
31             Y[i][0] = 1;
32             
33             Y[i][i] = 1;
34             
35          }
36          
37          for(int i = 2;i < m + 1;i++)
38              
39          {
40              
41              for(int j = 1;j < i;j++)
42                  
43              {
44                  
45                  Y[i][j] = Y[i - 1][j - 1] + Y[i - 1][j];
46                  
47              }
48              
49          }
50          
51          return Y[m][n];
52          
53     }
54      
55 }

程序測試截圖:

技術分享
(3)使用遞歸的方法用組合數遞推公式計算

程序設計思想:

定義求組合數函數C(n,k)當k = 0或n = k時,返回1,否則,根據遞推公式,C(n,k) = C(n - 1,k - 1) + C(n - 1,k)。

程序流程圖:

技術分享

程序源代碼:

import java.util.Scanner;

public class ZuHeShu2 {

    public static void main(String[] args) {
        
        System.out.print("請輸入組合數中的底數m:");
        
        Scanner input = new Scanner(System.in);
        
        int import_m = input.nextInt();
        
        System.out.print("請輸入組合數中的階數n:");
        
        int import_n = input.nextInt();
        
        System.out.print("組合數的結果為:"+triangle(import_m,import_n));
        
    }

    public static long triangle(int m,int n)
    
    {
        
        if(m == n || n == 0)
            
            return 1;else
            
            return triangle(m - 1,n - 1)+triangle(m - 1,n);
        
    }

}

程序結果截圖:

技術分享

2.遞歸編程階乘解決漢諾塔問題。

程序設計思想:

當盤子的個數n=1時,直接從A移動到C;

n>1時,可將其分為三個步驟:

(1)將上面n-1個盤子從A座移到B座上。

(2)將剩下的一個盤子從A座移到C座上。

(3)將B座上的n-1個盤子從B座移動到C座上。

其中,(1),(3)可用遞歸函數實現,只是從哪一個座移到哪一個座不同。

程序流程圖:

技術分享

程序源代碼:

import java.util.Scanner;

public class HanoiTower {

    public static void main(String[] args) {
        
        System.out.print("請輸入盤子的個數:");
        
        Scanner input = new Scanner(System.in);
        
        int diskes = input.nextInt();
        
        System.out.println("挪動這"+diskes+"盤子的步驟為:");
        
        hanoi(diskes,‘A‘,‘B‘,‘C‘);
        
    }

    public static void hanoi(int n, char a, char b, char c) 
    
    {
        
        if(n == 1)
            
        {
            
            move(a,c);
            
        }
        
        else
            
        {
            
            hanoi(n-1,a,c,b);
            
            move(a,c);
            
            hanoi(n-1,b,a,c);
            
        }
        
    }

    public static void move(char a, char c)
    
    {
        
        System.out.println(a+"-->"+c);
        
    }

}

程序結果截圖:

技術分享

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

程序設計思想:

首先比較第一個字符與最後一個字符的關系,如果相等,利用遞歸比較第二個與倒數第二個字符的關系,直到比較完,中間如果有一對不相等,直接退出函數並輸出不是回文數。

程序流程圖:

技術分享

程序源代碼:

import java.util.Scanner;

public class HuiWen {
    
    public static void main(String[] args) {
        
        System.out.print("請輸入要判斷的字符串:");
        
        Scanner input = new Scanner(System.in);
        
        String teststring = input.next();
        
        int pre = 0,end = teststring.length() - 1;
        
        if(isPalindromestring(teststring,pre,end))
            
            System.out.println(teststring+"是回文字符串!");
        
        else
            
            System.out.println(teststring+"不是回文字符串!");

    }

    public static boolean isPalindromestring(String test,int pre, int end)
    {
        
        if(pre >= end)
            
            return true;
        
        else
            
        {
            
            if(test.charAt(pre) == test.charAt(end))
                
                return isPalindromestring(test,pre+1,end-1);
            
            else
                
                return false;
            
        }
        
    }

}

程序測試截圖:

技術分享

技術分享

02-方法 ——課程作業01-遞歸練習