汉诺塔-算法实现
2010-09-18 19:03
211 查看
使用递归方法来实现经典算法问题<汉诺塔问题>。
下面是本人的代码:
// 汉诺塔.cpp : Defines the entry point for the console application.
//将A中的圆块移动到C中。
#include "stdafx.h"
#include<stdio.h>
void move(char,char);
void move(char,char);
void main()
{
int m;
printf("input the number of disks:");
scanf("%d",&m);
printf("the step to moving %3d diskes:/n",m);
hanoi(m,'A','B','C');
}
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);
}
}
下面是本人的代码:
// 汉诺塔.cpp : Defines the entry point for the console application.
//将A中的圆块移动到C中。
#include "stdafx.h"
#include<stdio.h>
void move(char,char);
void move(char,char);
void main()
{
int m;
printf("input the number of disks:");
scanf("%d",&m);
printf("the step to moving %3d diskes:/n",m);
hanoi(m,'A','B','C');
}
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);
}
}
相关文章推荐
- 【数据结构与算法】汉诺塔算法——C语言递归实现
- PHP实现的解汉诺塔问题算法示例
- 用Swift实现汉诺塔算法
- 汉诺塔(hanoi)算法实现
- 汉诺塔问题的算法分析与实现(Java)
- 汉诺塔问题的算法分析与实现(Java)
- 【算法笔记】使用栈实现汉诺塔(Hanoi)经典算法
- 汉诺塔问题的java算法实现
- 算法思考--------汉诺塔c语言实现-------递归
- 【数据结构与算法】汉诺塔算法——java递归实现
- 【算法】递归法实现汉诺塔问题
- Java实现汉诺塔 -- JAVA 算法学习
- 算法----汉诺塔的递归实现
- Javascript学习(一)——汉诺塔迭代算法的实现过程
- 【数据结构与算法】(五) c 语言递归与汉诺塔实现
- 【数据结构与算法】汉诺塔算法——C语言递归实现
- C语言经典算法(十)——递归实现汉诺塔
- C++实现汉诺塔算法经典实例
- python实现算法题-汉诺塔
- 大学常见算法的java实现及思考-汉诺塔问题