C 汉罗塔

来源:互联网 发布:北美购房软件 编辑:程序博客网 时间:2024/06/02 15:48
 

#include<stdio.h> 
#include<time.h> 
#include<stdlib.h> 
#include<iostream>
using namespace std;
void Move(int arraya,int arrayb,char a,char b)
{
   cout<<a<<"------------->"<<b<<endl;

}
void Moved(int num,int a,int b,int c)
{
    if(num > 0)
    {
        Moved(num - 1,a,b,c); //move a num -1 to c use b
        Move(a,c,a,c);            //move a num  to c
        Moved(num - 1,b,c,a); //move b num -1 to c use a
    }   
}  
void move_disk(char src, char dst)
{
printf("%c ====> %c\n",src,dst);
}

void towers(int n, char src, char mid, char dst)
{

 if (n > 0)
 { 
 towers(n-1,src,dst,mid);
 move_disk(src,dst); //这里就输出移动路线了!
 towers(n-1,mid,src,dst);
}  

int main()
{
    Moved(5,'a','b','c');
    system("pause");
    return 0;
}   

原创粉丝点击