用C语言编程序解决汉诺塔问题

如题所述

第1个回答  2017-12-28
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

int count;
void movedisk(int,char,char,char);

int _tmain(int argc, _TCHAR* argv[])
{
int n;
printf("输入盘子的数量:");
scanf("%d",&n) ;
movedisk(n,'A','C','B');

printf("盘子数量%d的时候移动了%d次\n",n,count);
scanf("%d",&n) ;

}

void movedisk(int n,char from,char to,char tmp)
{
if(n>1)
movedisk(n-1,from,tmp,to);
printf("盘子 %d 号从柱子 %c 移动到了柱子 %c 上\n",n,from,to);
count++;
if(n>1)
movedisk(n-1,tmp,to,from);
}本回答被网友采纳
相似回答
大家正在搜