一个关于汉诺塔编程的问题
怎样将汉诺塔编程中移动盘子的步骤总数统计出来?
代码如下:
#include "stdio.h"
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 of 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);
}
问题点数:20、回复次数:1Top
1 楼jixingzhong(瞌睡虫·星辰)回复于 2006-06-04 09:33:45 得分 20
定义一个全局的变量 count :
(或者可以是 函数中 static 类型的)
int move_count, hanoi_count;
void hanoi(int n,char one,char two,char three)
{
void move(char x,char y);
hanoi_count==; //count++
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)
{
move_count==; /count++
printf("%c------------>%c\n",x,y);
}
Top




