溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c#的漢諾塔算法怎么寫

發(fā)布時(shí)間:2022-01-12 10:45:39 來源:億速云 閱讀:145 作者:iii 欄目:編程語言

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

#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)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI