fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int N;
  5. void recursion(int d, string from, string to, string another){
  6. if(d==1){
  7. cout << "Move ring "<< 1 << " from " << from << " to " << to << '\n';
  8. }else{
  9. recursion(d-1,from,another,to);
  10. cout << "Move ring "<< d << " from " << from << " to " << to << '\n';
  11. recursion(d-1,another,to,from);
  12. }
  13. }
  14. //Move ring 1 from A to C
  15. int main() {
  16. while(cin >> N){
  17. recursion(N,"A","C","B");
  18. cout << '\n';
  19. }
  20. }
Success #stdin #stdout 0s 5320KB
stdin
4
stdout
Move ring 1 from A to B
Move ring 2 from A to C
Move ring 1 from B to C
Move ring 3 from A to B
Move ring 1 from C to A
Move ring 2 from C to B
Move ring 1 from A to B
Move ring 4 from A to C
Move ring 1 from B to C
Move ring 2 from B to A
Move ring 1 from C to A
Move ring 3 from B to C
Move ring 1 from A to B
Move ring 2 from A to C
Move ring 1 from B to C