Skip to content

Instantly share code, notes, and snippets.

@ggarlic
Created December 11, 2013 06:42
Show Gist options
  • Save ggarlic/7905998 to your computer and use it in GitHub Desktop.
Save ggarlic/7905998 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void hannoi (int n, char A, char B, char C) {
if (n == 1) {
cout << "move one from " << A << " to " << C << endl;
}
else {
hannoi (n-1, A, C, B);
cout << "move one from " << A << " to " << C << endl;
hannoi (n-1, B, A, C);
}
}
//tail recursion
void tail (int n, char a, char b, char c) {
char tmp = '\0';
while (n > 0 ) {
tail (n-1,a,c, b);
cout << "move one from " << A << " to " << C << endl;
n--;
tmp = a;
a = b;
b = tmp;
}
}
int main() {
hannoi (30, 'A', 'B', 'C');
// tail (30, 'A', 'B', 'C');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment