gpt4 book ai didi

c - 了解汉诺塔的递归解决方案

转载 作者:太空狗 更新时间:2023-10-29 15:26:49 25 4
gpt4 key购买 nike

<分区>

谁能解释一下这个程序?我特别想知道参数是如何传递给函数 tower 的,以及递归是如何工作的。

代码如下:

#include<stdio.h>
#include<conio.h>

void main()
{
int n;
clrscr();
printf("Enter the no. of disks");
scanf("%d",&n);
tower(n,'S','D','T');
getch();
}

tower(int n,char SOURCE,char DEST,char TEMP)
{
if(n>0)
{
tower(n-1,SOURCE,TEMP,DEST);
printf("\nMove disk %d from %c to %c",n,SOURCE,DEST);
tower(n-1,TEMP,DEST,SOURCE);
}
return;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com