先要编译啊.
cc h.c
bb@cash:~$ ./a.out
input the number of diskes: 3
The step to moveing 3 diskes:
A-->B
A-->B
C-->A
A-->C
B-->C
B-->C
A-->B
不过结果不对.我给你改了一下:
#include<stdio.h>
void main()
{void hanoi(int n,char one,char two,char three)
int m
printf("input the number of diskes: ")
scanf("%d",&m)
printf("The step to moveing %d diskes: \n",m)
hanoi(m,'A','B','C')
}
void hanoi(int n,char one,char two,char three)
{void move(char x,char y)
if(n==1)
move(one,three)/*这里要改成这个样子.*/
else
{hanoi(n-1,one,three,two)
move(one,three)
hanoi(n-1,two,one,three)
}
}
void move(char x,char y)
{printf("%c-->%c\n",x,y)
}
这叫递归,当n=3时执行了hanoi( n - 1, one , three, two )这时你再想想一下看好哦
执行 hanoi( n - 1, one , three, two )
n=3 one=a two=b three=c
进入函数内咯
hanoi( int n, char one, char two, char three )
对照一下吧n=2 one=a two=c three=b
你别把形参和实参搞混了,这样说你会比较容易懂
char three='c'
有个函数int a(char one){里面随便是什么}
你说a(three)什么意思啊?
自己对照下
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)