练习汗诺塔的问题主要是解决函数的递归调用问题。
#include <stdio.h>
#include <string.h>
void hanno(int num,char ch1,char ch2,char ch3)
{
if(num == 1)
{
printf("from %c to %c\n",ch1,ch3);
}
else
{
hanno(num-1,ch1,ch3,ch2);
printf("from %c to %c\n",ch1,ch3);
hanno(num-1,ch2,ch1,ch3);
}
}
int main(void)
{
char ch1 = 'A';
char ch2 = 'B';
char ch3 = 'C';
int num = 0;
printf("please input a number:\n");
scanf("%d",&num);
hanno(num,ch1,ch2,ch3);
return 0;
}