首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

汉诺塔有关问题的改动

2012-03-19 
汉诺塔问题的改动教材上知识简单的对汉诺塔思路进行了输出,我想做如下改动:每一个步骤之前带上数字显示多

汉诺塔问题的改动
教材上知识简单的对汉诺塔思路进行了输出,我想做如下改动:每一个步骤之前带上数字显示多少步;同时计算函数调用了多少次;
源代码:#include<stdio.h>
void main()
{
void hanoi(int n,char one ,char two,char three);
int m;
printf("input the nnumber of the diskes:");
scanf("%d",&m);
printf("the steps to moving %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);
}
输出:*****
  *****(省略)
  A-->C
  A-->B(以下均省略)
  我想要的效果:
  1:A-->C
  2:A-->B........总共调用次数:*** 请问怎么改动?上述源代码?


[解决办法]

C/C++ code
#include<stdio.h>static int i=1;//首先在这儿定义一个全局的void main()
[解决办法]
C/C++ code
#include <stdlib.h>#include <stdio.h>unsigned long long step = 0;int main(){    void hanoi(int n,char one ,char two,char three);    int m;    printf("input the nnumber of the diskes:");    scanf("%d",&m);    printf("the steps to moving %d diskes:\n",m);    hanoi(m,'A','B','C');    printf ("总共调用次数:%d\n", step);    system("pause");    return 0;}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){    ++step;    printf ("%c-->%c\n",x,y);} 

热点排行