编程语言
首页 > 编程语言> > 汉诺塔程序详解

汉诺塔程序详解

作者:互联网

把A柱的碟盘移动到C柱上面

#include <stdio.h>
#include <stdlib.h>

void hanoi(int n, char a, char b, char c);

void hanoi(int n, char a, char b, char c)
{
	if(n == 1) {
	printf("%d:%c to %c, ", n, a, c);
	}
	else{
		hanoi(n-1, a, c, b);                      //把A上n-1个盘子通过借助辅助塔(C塔)移到了B上
		printf("%d:%c to %c, ", n, a, c);
		hanoi(n-1, b, a, c);                      //把B上n-1个盘子通过借助辅助塔(A塔)移到了C上
	}
}

int main(void)
{
	hanoi(3, 'A', 'B', 'C');
	return EXIT_SUCCESS;
}

用的是递归方法

标签:int,hanoi,void,程序,char,详解,汉诺塔,printf,include
来源: https://www.cnblogs.com/ccfwz/p/16530837.html