当前位置:编程学习 > C/C++ >>

问题九十一:汉诺塔

 


[plain]
#include <stdio.h> 
 
void hanol(int num, char a, char b, char c); 
 
int main() 

   int  num; 
   char a='A'; 
   char b='B'; 
   char c='C'; 
    
   printf("please enter a number:"); 
   scanf("%d", &num); 
    
   hanol(num, a, b, c); 
 
   return 0; 

void hanol(int num, char a, char b, char c) 

     if(num==1) 
     { 
         printf("Move disk %d from %c to %c\n",num, a, c); 
     } 
     else 
     { 
         hanol(num-1, a, c, b); 
         printf("Move disk %d from %c to %c\n",num, a, c); 
         hanol(num-1, b, a, c); 
     } 

#include <stdio.h>

void hanol(int num, char a, char b, char c);

int main()
{
   int  num;
   char a='A';
   char b='B';
   char c='C';
  
   printf("please enter a number:");
   scanf("%d", &num);
  
   hanol(num, a, b, c);

   return 0;
}
void hanol(int num, char a, char b, char c)
{
     if(num==1)
  {
      printf("Move disk %d from %c to %c\n",num, a, c);
  }
  else
  {
   hanol(num-1, a, c, b);
   printf("Move disk %d from %c to %c\n",num, a, c);
   hanol(num-1, b, a, c);
  }
}  

 

 \

 

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,