ValerianBenchea

HANOI Pb Info

Nov 8th, 2021 (edited)
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream fin("hanoi.in");
  5. ofstream fout("hanoi.out");
  6. void h(int n, char A, char B, char C)
  7. {
  8.     if(n==1)
  9.         fout<<A<<"->"<<C<<endl;
  10.     else
  11.     {
  12.         h(n-1, A, C, B);
  13.         h(1, A, B, C);
  14.         h(n-1, B, A, C);
  15.     }
  16. }
  17.  
  18. int main()
  19. {
  20.     int n, cnt=1;
  21.     fin>>n;
  22.     for(int i=0; i<n; i++)
  23.         cnt*=2;
  24.     fout<<cnt-1<<endl;
  25.     h(n, 'A', 'B', 'C');
  26.     return 0;
  27. }
Add Comment
Please, Sign In to add comment