1. 程式人生 > >課程作業03:用遞歸方法計算組合數、解決漢諾塔問題、判斷某個字符串是否回文

課程作業03:用遞歸方法計算組合數、解決漢諾塔問題、判斷某個字符串是否回文

java class ply math alt static multi 構造 strong

課後作業1:使用計算機計算組合數

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

程序設計思想:

設計並調用大數求階乘的方法結合組合數公式計算組合數的值。

程序流程圖:

技術分享

技術分享

程序源代碼:

//信1605-2 張晨陽 20160955
/*
* 設計目的:設計並調用大數階乘方法來計算組合數。
*/
package homework1;

import java.math.BigInteger;//引入BigInteger類包
import java.util.Scanner;//引入Scanner類包

public class Homework1
{
public static void main(String[] args)
{
System.out.print("請輸入組合數中的n和k:");
Scanner input=new Scanner(System.in);//為Scanner類創建對象input,調用System.in傳參
int n=input.nextInt();//調用input對象的方法輸入並將輸入的字符串轉化為整型存到n中
int k=input.nextInt();//調用input對象的方法輸入並將輸入的字符串轉化為整型存到k中
while(n<0||k<0||n<k)//判斷n,k的值是否合理
{
System.out.print("輸入錯誤,請重新輸入n和k的值:");
n=input.nextInt();
k=input.nextInt();
}
BigInteger c;//定義長整型變量c用於存取組合數結果
c=N(n).divide(N(k)).divide(N(n-k));//調用遞歸求大數階乘的方法
System.out.println("組合式運算結果為:"+c);
}
public static BigInteger N(int n)//遞歸求大數階乘的方法
{
if(n==0||n==1)
{
return BigInteger.valueOf(1);
}
else
{
return BigInteger.valueOf(n).multiply(N(n-1));//遞歸調用
}

}

}

結果截圖:

技術分享

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

程序設計思想:

使用遞推的方法創建楊輝三角型二維數組來計算組合數。

程序流程圖:

技術分享

技術分享

程序源代碼:

//信1605-2 張晨陽 20160955
/*
* 設計目的:使用遞推的方法創建楊輝三角型二維數組來計算組合數。
*/
package homework1;

import java.util.Scanner;//引用Scanner類包

public class Homework2
{
public static void main(String[] args)
{
System.out.print("請輸入組合數中n的值:");
Scanner input=new Scanner(System.in);//為Scanner類創建對象input,調用System.in傳參
int n=input.nextInt();//調用input對象的方法輸入並將輸入的字符串轉化為整型存到n中
System.out.print("請輸入組合數中k的值:");
int k=input.nextInt();//調用input對象的方法輸入並將輸入的字符串轉化為整型存到k中
while(n<0||k<0||n<k)//判斷n,k的值是否合理
{
System.out.print("輸入錯誤,請重新輸入n和k的值:");
n=input.nextInt();
k=input.nextInt();
}
long[][] a=new long[n+1][];//創建並為二維數組開辟行空間
for(int i=0;i<=n;i++)
{
a[i]=new long[n+1];//為每個行空間分別開辟列空間
}
a[0][0]=1;
for(int i=1;i<=n;i++)//為每個數組元素賦初值
{
for(int j=0;j<=n;j++)
{
if(j==0||j==n)
{
a[i][j]=1;
}
else
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
}
System.out.print("組合數運算結果:"+a[n][k]);//輸出結果
}

}

結果截圖:

技術分享

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

程序設計思想:

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

程序流程圖:

技術分享

技術分享

程序源代碼:

//信1605-2 張晨陽 20160955
/*
* 設計目的:使用遞歸的方法用組合數遞推公式計算組合數。
*/
package homework1;

import java.math.BigInteger;//引入BigInteger類包
import java.util.Scanner;//引入Scanner類包

public class Homework3
{
public static void main(String[] args)
{
System.out.print("請輸入組合數中n的值:");
Scanner input=new Scanner(System.in);//為Scanner類創建對象input,調用System.in傳參
int n=input.nextInt();//調用input對象的方法輸入並將輸入的字符串轉化為整型存到n中
System.out.print("請輸入組合數中k的值:");
int k=input.nextInt();//調用input對象的方法輸入並將輸入的字符串轉化為整型存到k中
while(n<0||k<0||n<k)//判斷n,k的值是否合理
{
System.out.print("輸入錯誤,請重新輸入n和k的值:");
n=input.nextInt();
k=input.nextInt();
}
System.out.print("組合數的結果為:"+N(n,k));//調用N方法
}
public static BigInteger N(int n,int k)//用遞歸的方法借助組合數遞推公式求組合數
{
if(n==k||k==0)
{
return BigInteger.valueOf(1);
}
else
{
BigInteger[][] a=new BigInteger[n+1][];//創建並為二維數組開辟行空間
for(int i=0;i<=n;i++)
{
a[i]=new BigInteger[n+1];//為每個行空間分別開辟列空間
}
a[n][k]=N(n-1,k-1).add(N(n-1,k));//遞歸調用並求和
return a[n][k];
}
}

}

結果截圖:

技術分享

課後作業2:用遞歸編程解決漢諾塔問題

程序設計思想:

  假設只有一個盤子,那麽只需實現 A->C 這個動作;

  如果有兩個盤子,那麽需要

  (1)A->B;

  (2)A->C;

  (3)B->C;

  如果有三個盤子,可以將前兩個盤子看作一個盤子,對兩個盤子重復以上三個步驟,於是得到N個盤子的遞歸算法,遞歸結束的條件是N=1;

程序流程圖:

技術分享

程序源代碼:

//信1605-2 張晨陽 20160955
/*
* 設計目的:遞歸調用編程解決漢諾塔問題。
*/
package homework1;

import java.util.Scanner;//引入Scanner類包

public class Homework4
{
public static void main(String[] args)
{
System.out.print("請輸入盤子數n的值:");
Scanner input=new Scanner(System.in);//為Scanner類創建對象input,調用System.in傳參
int n=input.nextInt();//調用input對象的方法輸入並將輸入的字符串轉化為整型存到n中
moveDish(n, ‘A‘, ‘B‘, ‘C‘); //調用moveDish方法
}
public static void moveDish(int l, char f, char i, char t) //創建moveDish方法
{
if (l == 1)
{
System.out.println("從" + f + " 移動盤子" + l + " 到" + t);
} else {
moveDish(l - 1, f, t, i);
System.out.println("從" + f + " 移動盤子" + l + " 到" + t);
moveDish(l - 1, i, f, t);
}
}
}

結果截圖:

技術分享

技術分享

課後作業3:使用遞歸方式判斷某個字符串是否回文

程序設計思想:

字符串直接調用Scanner輸入即可,這道題目重點在於判斷是否為回文字符串的方法構建,用str.length()判斷長度,str.charAt()接收字符,判斷是否為回文字符串,找好對應位置是關鍵,a=str.length()-n, b=str.length()-(a+1),兩者一個從頭開始,一個從尾部開始,相向而行,運用遞歸算法判斷對應位置的值是否一樣,當兩者一樣,進行一次確定,只有一個字符就是回文數。

程序流程圖:

技術分享

程序源代碼:

//信1605-2 張晨陽 20160955
/*
* 設計目的:使用遞歸方式判斷某個字串是否回文。
*/
package homework1;

import java.util.Scanner;//引入Scanner類包

public class Homework5
{
public static void main(String[] args)
{
System.out.println("請輸入一個字符串:");
Scanner input=new Scanner(System.in);//為Scanner類創建對象input,調用System.in傳參
String str=input.next();//將輸入的字符串存到str中
int i=str.length();//計算str的長度存到i中
int j=panDuan(str,i);//調用panDuan方法,用j控制開關
if(j==1)
{
System.out.println("該字符串是回文的");
}
else
{
System.out.println("該字符串不是回文的");
}
}

public static int panDuan(String str,int n)//構造判斷字符串是否回文的方法
{
int a,b,t=0;
char p,q;
a=str.length()-n;
b=str.length()-(a+1);
p=str.charAt(a);
q=str.charAt(b);
if(p==q||a==b)
{
t=1;
}
if(a!=b&&a<b&&t==1)
{
panDuan(str,n-1);
}
return t;
}
}

結果截圖:

技術分享

課程作業03:用遞歸方法計算組合數、解決漢諾塔問題、判斷某個字符串是否回文