Advertisement
eddiehy

hanoi_move_times

Nov 29th, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int hanoi(int n,char from, char mid,char to,int* pCounter)
  4. {
  5. if(n==0)
  6. {return;}
  7. hanoi(n-1,from, to, mid,pCounter);
  8. //printf("圓盤%2d移動%d次\n",n,counter);
  9. pCounter[n-1]++;
  10. printf("plate%2d from %c -> %c\n",n,from,to,pCounter);
  11. hanoi(n-1,mid, from, to,pCounter);
  12. }
  13. int main()
  14. {
  15. int n;
  16. int pCounter[100]={0};
  17. scanf("%d",&n);
  18. hanoi(n,'A','B','C',pCounter);
  19. printf ("\n");
  20. int i;
  21. for (i=0;i<n;i++)
  22. {
  23. printf("plate %d = %d\n",i+1,pCounter[i]);
  24. }
  25. return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement