fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. void hanoi_tower(int n, char from, char tmp, char to)
  4. {
  5. if( n==1 ) printf("%c %c\n",from,to);
  6. else {
  7. hanoi_tower(n-1, from, to, tmp);
  8. printf("%c %c\n",from, to);
  9. hanoi_tower(n-1, tmp, from, to);
  10. }
  11. }
  12. int main(void)
  13. {
  14. int i;
  15. scanf("%d",&i);
  16. printf("%d\n",(int)pow(2,i)-1);
  17. hanoi_tower(i, '1', '2', '3');
  18. return 0;
  19. }
Success #stdin #stdout 0s 5312KB
stdin
3
stdout
7
1 3
1 2
3 2
1 3
2 1
2 3
1 3