JAVA第二章課後作業

來源:互聯網
上載者:User

標籤:lin   oid   漢諾塔   err   alt   images   sof   結束   toc   

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

 

設計思想:利用遞迴法求階乘,利用公式即可求得組合數的計算結果

import java.util.*;

public class CombinationNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("請輸入組合數下標n:");
int n = in.nextInt();
System.out.print("請輸入組合數上標k:");
int k = in.nextInt();
int C = Jiecheng(n)/(Jiecheng(k)*Jiecheng(n - k));
System.out.println("C(n,k) = "+C);
}
public static int Jiecheng(int n)//遞迴法計算階乘
{
int s = 0;
if(n < 0)
System.out.println("Error!");
else if(n == 1||n == 0)
s = 1;
else
s = n * Jiecheng(n -1);
return s;
}
}

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

設計思想:利用for迴圈逐步求得組合數結果。

import java.util.Scanner;

public class Combination2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("請輸入組合數下標n:");
int n = in.nextInt();
System.out.print("請輸入組合數上標k:");
int k = in.nextInt();

int C = CombinationNumber(n,k);
System.out.println("C(n,k) = "+C);
}
public static int CombinationNumber(int n,int k)
{
if(k == 0||n == k) return 1;
int s=Math.min(k,n-k);
int p = 1,q = 0;

for(int i = 1; i <= s; i++)//遞推計算
{
q = p * (n-i+1)/(i);
p = q;
}

return q;
}
}

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

設計思想:利用遞推公式,遞迴方式求得;注意結束條件。

import java.util.Scanner;

public class Combination3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("請輸入組合數下標n:");
int n = in.nextInt();
System.out.print("請輸入組合數上標k:");
int k = in.nextInt();

int C = CombinationNumber(n,k);
System.out.println("C(n,k) = "+C);
}
public static int CombinationNumber(int n,int k)//遞迴法計算群組合數
{
int c = 0;
if(n == 1||k == 0||n == k)
c = 1;
else
c = CombinationNumber(n - 1,k - 1) + CombinationNumber(n - 1,k);
return c;
}
}

2.遞迴編程解決漢諾塔問題

設計思想:當A塔上有n個盤子時,先將A塔上編號 1 至 n-1 的盤子移動到B塔上(藉助C塔),然後將A塔上n號盤子移動到C塔上,最後將B塔上的n-1個盤子藉助A塔移動到C塔上。即用遞迴實現,結束條件是 n=1 時只需直接移動;遇到的問題:如何遞迴實現。

import java.util.*;
public class TowersOfHanoi2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("請輸入盤子個數:");//盤子從上至下編號從1到n
int p = in.nextInt();
char ch1 = ‘A‘;
char ch2 = ‘B‘;
char ch3 = ‘C‘;
Hanoi(p,ch1,ch2,ch3);//最終實現1到n個盤子從A柱移到C柱
}
public static void move(int n,char from,char to)
{
System.out.printf( "將%d號盤子%c --> %c\n",n,from,to);
}
public static void Hanoi(int n,char from,char mid,char to)
{
if(n == 1)
move(1,from,to);//結束條件,當n = 1時移動一次即可
else
{Hanoi(n-1,from,to,mid);//將n-1個盤子從A移到B
move(n,from,to);//將n個盤子移到C
Hanoi(n-1,mid,from,to);//將n-1個盤子從B移到C
}
}
}

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

設計思想:將輸入的字串轉化為數組形式儲存才能方便比較(利用 toCharArray() 函數)

import java.util.*;
public class Palindrome {

public static void main(String[] args) {
// TODO Auto-generated method stub
String temp = "";
char[] a;
Scanner in = new Scanner(System.in);
System.out.print("請輸入要判斷的字串:");
String str = in.next();
a = str.toCharArray();//將字串對象中的字元轉換為一個字元數組
for(int i = a.length - 1;i >= 0;i--)//將字串逆置
{
temp += a[i];
}
if(str.equals(temp))
System.out.println(str+"是迴文數!");
else
System.out.println(str+"不是迴文數!");
}
}

 

 

JAVA第二章課後作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.