Hanoi


// 工程名:Hanoi
// 文件名:Hanoi.cpp
// 功能:汉诺塔递归求解
// 依赖文件:无

#include <iostream.h>
#include <string.h>

int count=0;

void Hanoi(int n,char A,char B,char C)
{
 if(n==1)
 { 
  count++;
  cout<<"第"<<count<<"步:";
  cout<<A<<"->"<<C<<endl;
 }
 else
 {
  Hanoi(n-1,A,C,B); //调用时不用写char
  count++;
  cout<<"第"<<count<<"步:";
  cout<<A<<"->"<<C<<endl;
  Hanoi(n-1,B,A,C);
 }
}

void main()
{
 int n;
 char a='A',b='B',c='C';
 cout<<"请输入汉诺塔层数:";
 cin>>n;
 Hanoi(n,a,b,c);
 // Hanoi(n,1,2,3);


原文链接: http://blog.csdn.net/t0nsha/article/details/1666220

你可能感兴趣的:(Hanoi)