fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void honai(int n, char current, char tmp, char target){
  5. if(n == 0) return;
  6. honai(n - 1, current, target, tmp);
  7. cout << "Move ring " << n << " from " << current << " to " << target << '\n';
  8. honai(n - 1, tmp, current, target);
  9. }
  10. int main() {
  11. int n;
  12. while(cin >> n){
  13. honai(n, 'A', 'B', 'C');
  14. cout << '\n';
  15. }
  16. return 0;
  17. }
Success #stdin #stdout 0.01s 5328KB
stdin
1
2
3
stdout
Move ring 1 from A to C

Move ring 1 from A to B
Move ring 2 from A to C
Move ring 1 from B to C

Move ring 1 from A to C
Move ring 2 from A to B
Move ring 1 from C to B
Move ring 3 from A to C
Move ring 1 from B to A
Move ring 2 from B to C
Move ring 1 from A to C