中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C語(yǔ)言經(jīng)典算法 - 三色河內(nèi)塔

2019-07-23    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
#include <stdio.h>
void hanoi(int disks, char source, char temp, char target)
{
  if (disks == 1)
  {
    printf("move disk from %c to %c\n", source, target);
    printf("move disk from %c to %c\n", source, target);
    printf("move disk from %c to %c\n", source, target);
  }
  else
  {
    hanoi(disks - 1, source, target, temp);
    hanoi(1, source, temp, target);
    hanoi(disks - 1, temp, source, target);
  }
}
 
void hanoi3colors(int disks)
{
  char source = 'A';
  char temp = 'B';
  char target = 'C';
  int i;
  if (disks == 3)
  {
    printf("move disk from %c to %c\n", source, temp);
    printf("move disk from %c to %c\n", source, temp);
    printf("move disk from %c to %c\n", source, target);
    printf("move disk from %c to %c\n", temp, target);
    printf("move disk from %c to %c\n", temp, source);
    printf("move disk from %c to %c\n", target, temp);
    ;
  }
  else
  {
    hanoi(disks / 3-1, source, temp, target);
    printf("move disk from %c to %c\n", source, temp);
    printf("move disk from %c to %c\n", source, temp);
    printf("move disk from %c to %c\n", source, temp);
    hanoi(disks / 3-1, target, temp, source);
    printf("move disk from %c to %c\n", temp, target);
    printf("move disk from %c to %c\n", temp, target);
    printf("move disk from %c to %c\n", temp, target);
    hanoi(disks / 3-1, source, target, temp);
    printf("move disk from %c to %c\n", target, source);
    printf("move disk from %c to %c\n", target, source);
    hanoi(disks / 3-1, temp, source, target);
    printf("move disk from %c to %c\n", source, temp);
    for (i = disks / 3-1; i > 0; i--)
    {
      if (i > 1)
      {
        hanoi(i - 1, target, source, temp);
      }
      printf("move disk from %c to %c\n", target, source);
      printf("move disk from %c to %c\n", target, source);
      if (i > 1)
      {
        hanoi(i - 1, temp, source, target);
      }
      printf("move disk from %c to %c\n", source, temp);
    }
  }
}
 
int main()
{
  int n;
  printf("請(qǐng)輸入盤數(shù):");
  scanf("%d", &n);
  hanoi3colors(n);
  return 0;
}

標(biāo)簽: [db:TAGG]

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:CSS實(shí)現(xiàn)三列圖片等寬等間距布局

下一篇:JavaScript實(shí)現(xiàn)圖片輪播