⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 换位递归.c

📁 数据结构主要常用算法以及一些必要的算法说明
💻 C
字号:
#include <stdio.h>

void move(char x,char y) 
{printf("%c-->%c\n",x,y);} 
void hanoi (int n,char one ,char two,char three) 
{
if(n==1) move (one ,three); 
else 
{
hanoi (n-1,one,three,two); 
move(one,three); 
hanoi(n-1,two,one,three); 
} 
} 
main() 
{int m; 
printf("input the number of diskes:"); 
scanf("%d",&m); 
printf("the step to moving %3d diskes:\n",m); 
hanoi(m,'A','B','C'); 
} 
/*运行情况如下: 
input the number of diskes:3 回车 
the step to moving 3 diskes: 
A-->C 
A-->B 
C-->B 
A-->C 
B-->A 
B-->C 
A-->C 

书上说hanoi(n-1,one,three,two);是把“one”上的n-1个往“two”上移,接着move(one,three);然后是hanoi(n-1,two,one,three)即把“two”上的n-1个往“three”上移;
           |h(2,1,3,2)|h(1,1,2,3)=>move(1,3)   <-----1------ 
           |          |  move(1,2)             <-----2------ 
           |          |h(1,3,1,2)=>move(3,2)   <-----3------ 
           |move(1,3)                          <-----4------ 
           | 
h(3,1,2,3) |          |h(1,2,3,1)=>move(2,1)   <-----5------ 
           |h(2,2,1,3)|move(2,3)               <-----6------- 
           |          |h(1,1,2,3)=>move(1,3)       <-----7------ 
           | 
*/ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -