Created
December 11, 2013 06:42
-
-
Save ggarlic/7905998 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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