河內塔

#include <stdio.h>
void hanoi (int n, char A, char B, char C)
{
  if (n>0) {
    hanoi (n - 1, A, C, B);
    cout << "移動盤子 " << n << " 從柱 " << A << " 到柱 " << C << endl;
    hanoi( n-1, B, A, C);
  }
}

int main() {
    int n;
    printf("請輸入盤數:");
    scanf("%d", &n);
    hanoi(n, 'A', 'B', 'C');
    return 0;
}

results matching ""

    No results matching ""