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