towers of hanoi

1
#include <iostream> using namespace std; void hanoi(int n, string source, string destination, string help); main() { int disks; cout << "How many disks? "; cin >> disks; hanoi(disks, "Pole 1", "Pole 2", "Pole 3"); system("PAUSE > NUL"); } void hanoi(int n, string source, string destination, string help) { if (n == 1) { cout << "Move 1 disk from " << source << " to " << destination << endl; } else { hanoi(n - 1, source, help, destination); cout << "Move 1 disk from " << source << " to " << destination << endl; hanoi(n - 1, help, destination, source); } }

Upload: abdelrahman-bahieldin

Post on 06-Apr-2016

214 views

Category:

Documents


0 download

DESCRIPTION

C++ Program

TRANSCRIPT

Page 1: Towers of Hanoi

#include <iostream>

using namespace std;

void hanoi(int n, string source, string destination, string help);

main() { int disks; cout << "How many disks? "; cin >> disks; hanoi(disks, "Pole 1", "Pole 2", "Pole 3"); system("PAUSE > NUL");}

void hanoi(int n, string source, string destination, string help) { if (n == 1) { cout << "Move 1 disk from " << source << " to " << destination << endl; } else { hanoi(n - 1, source, help, destination); cout << "Move 1 disk from " << source << " to " << destination << endl; hanoi(n - 1, help, destination, source); }}