fork download
  1. #include <iostream>
  2.  
  3. int lineNumber;
  4. enum pole { left, middle, right };
  5.  
  6. void towersOfHanoi (int n, pole start, pole temporary, pole destination)
  7. {
  8. if (n > 0) {
  9. towersOfHanoi(n-1, start, destination, temporary);
  10. lineNumber++;
  11. std::cout << lineNumber << ". Move the top from the " << start
  12. << " pole to the " << destination << " pole.\n";
  13. towersOfHanoi(n-1, temporary, start, destination);
  14. }
  15. }
  16.  
  17. void towersOfHanoi(
  18. int d)
  19. {
  20. lineNumber = 0;
  21. towersOfHanoi(d, left, middle, right);
  22. }
  23.  
  24.  
  25. std::ostream& operator<< (
  26. std::ostream& os,
  27. pole p)
  28. {
  29. switch(p) {
  30. case left: os << "left"; break;
  31. case middle: os << "middle"; break;
  32. case right: os << "right"; break;
  33. }
  34. return os;
  35. }
  36.  
  37. int main()
  38. {
  39. towersOfHanoi(3);
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
1. Move the top from the 0 pole to the 2 pole.
2. Move the top from the 0 pole to the 1 pole.
3. Move the top from the 2 pole to the 1 pole.
4. Move the top from the 0 pole to the 2 pole.
5. Move the top from the 1 pole to the 0 pole.
6. Move the top from the 1 pole to the 2 pole.
7. Move the top from the 0 pole to the 2 pole.