# 不知道哪里错了， 大侠帮忙debug看看

ylxin1993 2013-02-08 07:48:14
debug的时候到Knapsack 函数的for( int i = 0; i < n; ++i)这句就中止了也不知道为什么， 大侠帮忙看看。。.

``````
#include <fstream>

#include <iostream>

#include <cmath>

#include <fstream>

using namespace std;

int Knapsack(int value[], int weight[], int n, int w);

int main()

{

int B, size, *value, *weight;

int mostValue = 0;

ifstream inFile("Knapsack.txt");

if( inFile.is_open() ) {

cout << "Open file success!\n";

inFile >> B;

inFile >> size;

value = new int[size];

weight = new int[size];

for( int i = 0; i < size; ++i ) {

inFile >> value[i];

inFile >> weight[i];

}

} else {

cerr << "Open file failure.\n";

return 0;

}

inFile.close();

cout << "Here's the most value: ";

mostValue = Knapsack(value, weight, size, B);

cout << "Most Value = " << mostValue << endl;

return 0;

}

int Knapsack(int value[], int weight[], int n, int w) {

int v[n][w];

for( int i = 0; i < n; ++i)

v[i][0] = 0;

for( int j = 0; j < w; ++j)

v[0][j] = 0;

for( int i = 1; i <= n; ++i ) {

for( int j = 1; j <= w; ++j ) {

v[i][j] = v[i - 1][j];

if( weight[i] <= j )

v[i][j] = max( v[i][j], v[i - 1][j - weight[i]] + value[i] );

}

}

return v[n][w];

}

``````
...全文
134 7 点赞 打赏 收藏 举报

7 条回复

ylxin1993 2013-02-10

int v[n][w];这句能编译通过吗？

• 打赏
• 举报

ylxin1993 2013-02-10

• 打赏
• 举报

ylxin1993 2013-02-10

I've said in the beginning: debug的时候到Knapsack 函数的for( int i = 0; i < n; ++i)这句就中止了
• 打赏
• 举报

lee_鹿游原 2013-02-09

• 打赏
• 举报

dingqiang107 2013-02-09
int v[n][w];这句能编译通过吗？
• 打赏
• 举报

flydreamforever 2013-02-08

• 打赏
• 举报

ylxin1993 2013-02-08
Knapsack.txt: 18000 1000 26 16 29 27 32 16 30 33 19 47 49 47 47 25 5 33 6 48 27 30 35 28 36 9 16 44
• 打赏
• 举报

6.0w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等

2013-02-08 07:48