fork download
  1. #include <stdio.h>
  2. void moveOneDisk(int from, int to)
  3. {
  4. printf("杭%dから杭%dに移動\n", from, to);
  5. }
  6. void moveDisks(int from, int to , int n)
  7. {
  8. int anotherPole = 6-(from+to);
  9. if(n >= 1)
  10. {
  11. moveDisks(from,anotherPole,n-1);
  12. moveOneDisk(from,to);
  13. moveDisks(anotherPole,to,n-1);
  14. }
  15. }
  16. int main(void)
  17. {
  18. int n = 3;
  19. printf("%d枚の円盤を杭1から杭3に移動させる手順は以下のとおり:\n",n);
  20. moveDisks(1, 3, n);
  21. return 0;
  22. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
3枚の円盤を杭1から杭3に移動させる手順は以下のとおり:
杭1から杭3に移動
杭1から杭2に移動
杭3から杭2に移動
杭1から杭3に移動
杭2から杭1に移動
杭2から杭3に移動
杭1から杭3に移動