Hanoi塔问题(C)
2007-03-02 11:43
288 查看
#include<stdio.h>
void move(unsigned n,char fromd,char tod,char us);
int i=0;
int main()
{
unsigned n;
int j;
printf("请输入盘子数:");
scanf("%d",&n);
printf(" 位置 : a b c ");
move(n,'a','c','b');
printf(" 共计: %d ",i);
scanf("%d",&n);
return 0;
}
void move(unsigned n,char fromd,char tod,char us)
{
if(n>0)
{
move(n-1,fromd,us,tod);
++i;
switch(fromd)
{
case 'a':
switch(tod)
{
case 'b':
printf(" 第[%d]步: %2d----->%2d ",i,n,n);
break;
case 'c':
printf(" 第[%d]步: %2d------------->%2d ",i,n,n);
break;
}
break;
case 'b':
switch(tod)
{
case 'a':
printf(" 第[%d]步: %2d<-----%2d ",i,n,n);
break;
case 'c':
printf(" 第[%d]步: %2d----->%2d ",i,n,n);
break;
}
break;
case 'c':
switch(tod)
{
case 'a':
printf(" 第[%d]步: %2d<-------------%2d ",i,n,n);
break;
case 'b':
printf(" 第[%d]步: %2d<-----%2d ",i,n,n);
break;
}
break;
}
move(n-1,us,tod,fromd);
}
}
/*
编写一个递归程序,解决Hanoi塔问题。要求盘数通过键盘输入控制,打印移动各盘的顺序。*/
void move(unsigned n,char fromd,char tod,char us);
int i=0;
int main()
{
unsigned n;
int j;
printf("请输入盘子数:");
scanf("%d",&n);
printf(" 位置 : a b c ");
move(n,'a','c','b');
printf(" 共计: %d ",i);
scanf("%d",&n);
return 0;
}
void move(unsigned n,char fromd,char tod,char us)
{
if(n>0)
{
move(n-1,fromd,us,tod);
++i;
switch(fromd)
{
case 'a':
switch(tod)
{
case 'b':
printf(" 第[%d]步: %2d----->%2d ",i,n,n);
break;
case 'c':
printf(" 第[%d]步: %2d------------->%2d ",i,n,n);
break;
}
break;
case 'b':
switch(tod)
{
case 'a':
printf(" 第[%d]步: %2d<-----%2d ",i,n,n);
break;
case 'c':
printf(" 第[%d]步: %2d----->%2d ",i,n,n);
break;
}
break;
case 'c':
switch(tod)
{
case 'a':
printf(" 第[%d]步: %2d<-------------%2d ",i,n,n);
break;
case 'b':
printf(" 第[%d]步: %2d<-----%2d ",i,n,n);
break;
}
break;
}
move(n-1,us,tod,fromd);
}
}
/*
编写一个递归程序,解决Hanoi塔问题。要求盘数通过键盘输入控制,打印移动各盘的顺序。*/
相关文章推荐
- Hanoi塔问题(递归解决)
- 算法分析——Hanoi塔问题
- Hanoi塔问题
- hanoi塔问题
- 算法----hanoi塔问题(Java)
- Hanoi塔问题 栈与递归C语言编程实现
- 一步一步写算法(之Hanoi塔问题)
- Hanoi塔问题
- Java Hanoi塔问题简单分治算法实现
- 数据结构:栈与递归(Hanoi塔问题)
- Hanoi塔问题
- 算法-Hanoi塔问题+递归
- hanoi塔问题解析(二)
- 栈与递归的实现(Hanoi塔问题等等)
- hanoi塔问题分析
- hanoi塔问题的递归实现
- hanoi塔问题
- [导入]Hanoi塔问题(C)
- hanoi塔问题的自己的理解及相关竞赛题的解答
- 算法分析——Hanoi塔问题