c#的漢諾塔算法怎么寫

這篇“c#的漢諾塔算法怎么寫”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“c#的漢諾塔算法怎么寫”文章吧。

創(chuàng)新互聯(lián)公司專注于清水河企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城建設(shè)。清水河網(wǎng)站建設(shè)公司,為清水河等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

#include <stdio.h>
void main()
{
  extern a;
  extern b;
  extern c;
  extern s;
  void hanno(int,char,char,char);
  void setNum(int);
  int n;
  printf("Please input the number of dishes: ");
  scanf("%d",&n);
  printf("The following is moving %d dishes ...\n             (%2d  0  0)\n",n,n);
  setNum(n);
  hanno(n,'A','B','C');
}
int a;
int b;
int c;
int s;
void setNum(int m)
{
  a=m;
}
void hanno(int m,char pillar1,char pillar2,char pillar3)
{
  void move(char,char,int);
  if(m==1) {
    s++;
    move(pillar1,pillar3,s);
  } else {
    hanno(m-1,pillar1,pillar3,pillar2);
    s++;
    move(pillar1,pillar3,s);
    hanno(m-1,pillar2,pillar1,pillar3);
  };
}
void move(char x,char y,int s)
{
  if (x=='A' && a>0 ) a--;
  else if (x=='B' && b>0 ) b--;
  else if (x=='C' && c>0 ) c--;
  
  if (y=='A') a++;
  else if (y=='B') b++;
  else if (y=='C') c++;
  printf("%3d: %c --> %c (%2d %2d %2d)\n",s,x,y,a,b,c);
}

#gcc hannuota.c -o hannuota

# ./hannuota
Please input the number of dishes: 3
The following is moving 3 dishes ...
             ( 3  0  0)

  1: A --> C ( 2  0  1)
  2: A --> B ( 1  1  1)
  3: C --> B ( 1  2  0)
  4: A --> C ( 0  2  1)
  5: B --> A ( 1  1  1)
  6: B --> C ( 1  0  2)
  7: A --> C ( 0  0  3)

以上就是關(guān)于“c#的漢諾塔算法怎么寫”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)題目:c#的漢諾塔算法怎么寫
URL網(wǎng)址:http://muchs.cn/article32/jpepsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、ChatGPT網(wǎng)站營(yíng)銷、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)