关于汉诺塔,数组溢出,输出顺序不对,如何更改
程序代码:
#include<iostream> #include<iomanip> const int PAN=4; using namespace std; void move(int a[], int b[]) //移动盘子 { int i, j; i = PAN - 1; j = PAN - 1; while (i>=0&&a[i]>0) i--; i++; while(b[j]>0) j--; b[j] = a[i]; a[i] = 0; } void hanoi(int n, int A[], int B[], int C[]) { if (n == 1) move(A, C); else { hanoi(n - 1, A, B, C); move(A, C); hanoi(n - 1, B, A, C); } } int main() { int aa[PAN]; int bb[PAN] = {0}; int cc[PAN] = {0}; int i, j; //給盘子aa赋初值 for (i = 0; i < PAN; i++) aa[i] = i+1; hanoi(PAN, aa, bb, cc); //输出cc数组 for (j = 0; j < PAN; j++) cout <<setw(2) << cc[j]; cout << endl; return 0; }
请问是哪点出了问题,如何修改