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

HanoiProblem/은지

From ZeroWiki
Revision as of 05:23, 7 February 2021 by imported>Unknown
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
#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