汉诺塔

C语言递归求解汉诺塔

Talk is easy, Show you code!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* 
   思路: 1.将 n-1个盘子先放到B座位上
   2.将A座上地剩下的一个盘移动到C盘上
   3、将n-1个盘从B座移动到C座上 */
unsigned long long int count = 1;
void hannuo(int n, char A, char B, char C)
{
	if (n == 1)
		printf("第%ld步:把%c上的第%d个盘子移动到--->%c柱子上\n", count++, A, n,
			   C);
	else
	{
		hannuo(n - 1, A, C, B);
		printf("第%ld步:把%c上的第%d个盘子移动到--->%c柱子上\n", count++, A, n,
			   C);
		hannuo(n - 1, B, A, C);

	}
}

int main()
{

	clock_t start, finish;
	double duration;
	int n;
	printf("输入汉诺塔层数\n");
	scanf("%d", &n);
	start = clock();

	// 计时开始
	hannuo(n, 'A', 'B', 'C');
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	// 计时结束
	printf("\n\n程序运行总共花了:%lfms\n", duration);
	return 0;

}
updatedupdated2020-05-032020-05-03