代码实现
代码
#include<stdio.h>
intstep=0;
voidhanoi(intn,charstart,charassist,charend){
if(n>=1){
hanoi(n-1,start,end,assist);
printf("move%dfrom%c-->%c
",n,start,end);
step++;
hanoi(n-1,assist,start,end);
}
}
intmain(){
intn;
scanf("%d",&n);
hanoi(n,'A','B','C');
printf("Totallymove%dsteps
",step);
return 0;
}
运行结果
Pleaseinputthedisknum:
3
move1fromA-->C
move2fromA-->B
move1fromC-->B
move3fromA-->C
move1fromB-->A
move2fromB-->C
move1fromA-->C
Totallymove7steps
优质内容筛选与推荐>>
1、FPGA作为从机与STM32进行SPI协议通信---Verilog实现2、git初使用3、实验4、从取球问题到含重复的组合问题模板5、ZOJ3554 A Miser Boss(dp)