하노이타워

 

#include<iostream>
#include<iomanip>

using namespace std;

void tower(int n, char source, char dest, char aux);

int main()
{
 int numDisks;
 cout<<"디스크 몇개?";
 cin>> numDisks;
 cout<<"하노이의 탑 시작"<<endl;

 tower(numDisks, 'A', 'C', 'B');

}
void tower(int n, char source, char dest, char aux)
{
 static int step=0; //항상유지되길 원하면 Static사용해라!!

 cout<<"Tower (" << n << " , " << source << "," << dest << "," << aux<<")" <<endl;

 if(n==1)
 {
  cout << "\t\t\t스텝" <<++step << source << "에서" << dest << "로 옮김" << endl;
 }
 else
 {
  tower(n-1, source, aux,dest);

  cout << "\t\t\t스텝" <<++step << source << "에서" << dest << "로 옮김" << endl;

  tower(n-1, aux,dest,source);
 }
 return;

}

'1. IT Story > Development' 카테고리의 다른 글

AddressListing  (0) 2012.03.29
링크드리스트ADT를 이용한 학생성적관리 Pro  (0) 2012.03.29
insertionSort  (0) 2012.03.28
Heap Tree (보기좋게출력)  (0) 2012.03.28
이진찾기  (0) 2012.03.28
피보나치  (0) 2012.03.28
윈도우폰 게임개발 기초(WindowPhoneGame)  (0) 2012.03.07
C# 채팅 프로그램(Client 확장)  (0) 2012.03.07
블로그 이미지

운명을바꾸는자

IT와 함께 살아가는 삶

,