U E D R , A S I H C RSS

하노이탑/조현태

하노이의 탑..^^
~cpp 
#include <iostream>

int answer=0;
void hanoi(int, int, int, int);
using namespace std;
void main()
{
	int number;
	
	cout << "하노이의 탑 블럭의 개수를 입력해주세요 >>";
	cin >> number;
	hanoi (1,3,2,number);
	cout << "답은 "<< answer << "입니다.";
}

void hanoi(int from, int middle, int target, int num)
{
	++answer;
	if (1==num)
	{
		cout << from << "에서" << target << "으로 옮깁니다.\n";
	}
	else
	{
		hanoi (from, target, middle, num-1 );
		cout << from << "에서" << target << "으로 옮깁니다.\n";
		hanoi (middle, from, target, num-1 );
	}
}


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:29
Processing time 0.0099 sec