我將妳的main還數添加了壹些測試代碼,修改如下:
void main()
{
int n, c;
n = 5; c = 10;
int v[5] = {1,2,3,5,6};
int w[5] = {10,20,9,8,1};
int m[10][10] = {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}};
int x[10] = {0};
cout << "n = " << n << endl;
cout << "c = " << c << endl;
cout << "The element in v[5]" << endl;
for (int i = 0; i < 5; i++)
{
cout << "v[" << i << "] = " << v[i] << " ";
}
cout << endl;
cout << "The element in w[5]" << endl;
for (int i = 0; i < 5; i++)
{
cout << "w[" << i << "] = " << w[i] << " ";
}
cout << endl;
cout << "The element in m[10][10]" << endl;
for (int i = 0; i < 10; i++)
{
cout << "m[" << i << "] : " << endl;
for (int j = 0; j < 10; j++)
{
cout << m[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "The element in x[10]" << endl;
for (int i = 0; i < 5; i++)
{
cout << "x[" << i << "] = " << x[i] << " ";
}
cout << endl;
Knapsack(v,w,c,n,m);
Traceback(m,w,c,n,x);
for(int i=0;i<10;i++)
cout<<x[i]<<endl;
cout<<"flag"<<endl;
printf("hello");
}
=============================================================
通過上面程序的運行似乎偶發現VS2005的壹個bug。
在debug的監視變量對話框中,幾個數組顯示的值和輸出來的還不壹樣,用cout打印出來的肯定沒問題。所以這可能監視變量有問題。剛開始我還以為數組的聲明格式有問題,存在數組越界覆蓋呢。這可能是vs2005的壹個bug。