日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java优秀算法河内之塔_河内塔的Java程序

發布時間:2023/12/1 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java优秀算法河内之塔_河内塔的Java程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java優秀算法河內之塔

Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using the third rod (say auxiliary). The rules are:

河內塔是一個數學難題,其中我們有三個桿和n個盤。 難題的目的是使用第三個桿(例如輔助桿)將所有磁盤從源桿移動到目標桿。 規則是:

  • Only one disk can be moved at a time.

    一次只能移動一個磁盤。

  • A disk can be moved only if it is on the top of a rod.

    僅當磁盤位于桿的頂部時才能移動磁盤。

  • No disk can be placed on the top of a smaller disk.

    不能將磁盤放置在較小磁盤的頂部。

  • Print the steps required to move n disks from source rod to destination rod. Source Rod is named as 'a', auxiliary rod as 'b' and destination rod as 'c'.

    打印將n個磁盤從源棒移到目標棒所需的步驟。 源桿稱為'a' ,輔助桿稱為'b' ,目的桿稱為'c' 。

    Input format: Integer n

    輸入格式:整數n

    Output format: Steps in different lines (in one line print source and destination rod name separated by space).

    輸出格式:不同行中的步驟(在一行中,打印源和目標桿名稱用空格分隔)。

    Example:

    例:

    Sample Input:2Sample Output:a ba cb c

    Explanation:

    說明:

    This is one of the famous problems on recursion. To solve this, let's assume the steps taken for 2 disks. Let’s assume Rod A, B, and C and we have to shift the disks from A to B. First, we shift the smaller disk to C, then we shift the larger disk to B, and at last, put the smaller disk from C to B.

    這是遞歸的著名問題之一。 為了解決這個問題,我們假設對2個磁盤采取了步驟。 假設桿A,B和C,我們必須將磁盤從A移到B。首先,將較小的磁盤移至C,然后將較大的磁盤移至B,最后,將較小的磁盤從C移入到B。

    Therefore, for N disks, lets recursion shifts N-1 disks to C, and we will shift the last disk to B and again let recursion shifts rest of the disk to C. Using this, we will be able to solve the problem.

    因此,對于N個磁盤,讓遞歸將N-1個磁盤移至C,然后將最后一個磁盤移至B,再讓遞歸將其余磁盤移至C。使用此方法,我們可以解決問題。

    Algorithm:

    算法:

    Declare a recursive function towerOfHanoi with parameters (int disk, char source, char auxiliary, char destination)

    聲明帶有參數(int磁盤,char源,char輔助,char目標)的遞歸函數towerOfHanoi

    • STEP 1: Base Case: If(disk == 0) return;

      步驟1:基本情況: If(disk == 0)返回;

    • STEP 2: Base Case: If(disk == 1) Print Source to Destination

      步驟2:基本情況: If(disk == 1)打印源到目標

    • STEP 3: Recursive Case: towerOfHanoi(disk -1, source, destination, auxiliary)

      步驟3:遞歸案例: towerOfHanoi(磁盤-1,源,目標,輔助)

    • STEP 4: Print Source to Destination

      步驟4:將源打印到目標

    • STEP 5: towerOfHanoi(disk -1, auxiliary, source,destination)

      步驟5: TowerOfHanoi(磁盤-1,輔助,源,目標)

    Example:

    例:

    Input : 3Disk 1 moved from A to CDisk 2 moved from A to BDisk 1 moved from C to BDisk 3 moved from A to CDisk 1 moved from B to ADisk 2 moved from B to CDisk 1 moved from A to C .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    Program:

    程序:

    import java.util.Scanner;public class Main {//Recursive Functionpublic static void towerOfHanoi(int disks, char source, char auxiliary, char destination) { // Write your code hereif(disks==0){ //Base Case 1return;}if(disks==1){ //Base Case 2System.out.println(source +" " + destination);return;}else{//Shifting d-1 disk from A to CtowerOfHanoi(disks-1,source,destination,auxiliary); System.out.println(source + " " + destination);//Shifting d-1 disk from c to BtowerOfHanoi(disks-1,auxiliary,source,destination); }}public static void main(String[] args) {int disk;Scanner s = new Scanner(System.in);System.out.print("Print No of Disks: ");disk = s.nextInt();towerOfHanoi(disk, 'A', 'C', 'B');} }

    Output

    輸出量

    Print No of Disks: 3 A B A C B C A B C A C B A B

    翻譯自: https://www.includehelp.com/java-programs/tower-of-hanoi.aspx

    java優秀算法河內之塔

    總結

    以上是生活随笔為你收集整理的java优秀算法河内之塔_河内塔的Java程序的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。