U E D R , A S I H C RSS

Hanoi Problem/은지

~cpp 

#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); 
	}
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:21
Processing time 0.0161 sec