数据结构实训:用C语言完成汉诺塔要有注释
#include <stdio.h>
//移动函数,将n个盘子从from柱子移动到to柱子,中间使用temp柱子辅助移动 void move(int n,char from,char to,char temp){ //如果只有一个盘子,则直接移动 if(n==1){ printf("Move disk 1 from %c to %c\n",from,to); return; } //否则,先将n-1个盘子从from移动到temp上 move(n-1,from,temp,to); //再将最大的盘子从from移动到to上 printf("Move disk %d from %c to %c\n",n,from,to); //最后将n-1个盘子从temp移动到to上 move(n-1,temp,to,from); }
int main(){ int n; printf("请输入盘子数:"); scanf("%d",&n); //调用移动函数 move(n,'A','C','B'); return 0; }
原文地址: https://www.cveoy.top/t/topic/bsUu 著作权归作者所有。请勿转载和采集!