Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

HanoiProblem/은지

From ZeroWiki
#include <iostream>
using namespace std;

void hanoi(int n, int from, int by, int to);

int main()
{
	int n;
	int from, by, to;
	
	cout << "=하노이탑 문제=\n";
	cout << "하노이탑 개수 입력 : ";
	cin >> n;

	hanoi(n, 1, 2, 3);
	return 0;

}

void hanoi(int n, int from, int by, int to)
{
	if(n==1)
		cout << from << "->" << to <<"\n";

	else
	{
		hanoi(n-1, from, to, by);
		hanoi(1, from, by, to);
		hanoi(n-1, by, from, to); 
	}
}

HanoiProblem