0-1背包问题 C++ 一直编译不通过 求大神指出错误

#include
using namespace std;

const int MAX = 250;
int V[MAX][MAX];

int max(int a, int b)
{
if (a >= b)
return a;
else
return b;
}

int Knapsack(int n[], int vi[], int c, int x,int choose[])
{
for (int i = 0; i <= x; i++)
V[i][0] = 0;
for (int j = 0; j <= c; j++)
V[0][j] = 0;

for (int i = 1; i <= x; i++)
{
    cout << i << "  " << n[i] << "  " << vi[i]<<"   ";

    for (int j = 1; j <= c; j++)
    {
        if (n[j] > j)
        {
            V[i][j] = V[i - 1][j];
            cout << "[" << i << "]" << "[" << j << "]" << "=" << V[i][j]<<" ";
        }

        else
        {
            V[i][j] = max(V[i - 1][j], V[i - 1][j - n[j]] + vi[i]);
            cout << "[" << i << "]" << "[" << j << "]" << "=" << V[i][j] << "   ";
        }

    }
    cout << endl;
}

int j = c;
for (int i = x; i > 0; i--)
{
    if (V[i][j] > V[i - 1][j])
    {
        choose[i] = 1;
        j = j - n[i];
    }
    else
        choose[i] = 0;
}
for (int i = 1; i <= x; i++)
    cout<< choose[i];
cout << endl;

return V[x][c];

}
int main()
{
int c, x;
cout << "请输入背包容量;";
cin >> c;
cout << "请输入物品个数:";
cin >> x;
int *n = new int[x+1];
int *vi = new int[x+1];
int *choose = new int[x+1];
cout << "请分别输入物品重量";
for (int i = 1; i <= x; i++)
cin >> n[i];
cout << "请分别输入物品价值:";
for (int i = 1; i <= x; i++)
cin >> vi[i];
int k = Knapsack(n, vi, c, x,choose);
cout << "物品最大价值为:" << k << endl;

}

2个回答

 #define max(a,b)            (((a) > (b)) ? (a) : (b))

int max1(int a, int b)  

定义与#define的相同了。。。

baidu_38729492
baidu_38729492 回复人类新纪元开始了: 大佬-。-已经修改函数名了--还是不行-。-对了 if (n[j] > j) 发现错误。。改为n[i]了 结果还是运行到一般 崩溃-。-
接近 3 年之前 回复
shen_wei
shen_wei int max1(int a, int b) 修改函数名字就可以了。。。不要与宏定义的相同
接近 3 年之前 回复
baidu_38729492
baidu_38729492 麻烦大佬再说细一点行么--我刚刚试了试大佬 说的-。-还是编译崩溃啊--我感觉是Knapsack 函数里面的有东西不对-。-
接近 3 年之前 回复
 const int MAX = 250;
int V[MAX][MAX];
int max1(int a, int b)
{
    if (a >= b)
        return a;
    else
        return b;
}
int Knapsack(int n[], int vi[], int c, int x,int choose[])
{
    for (int i = 0; i <= x; i++)
        V[i][0] = 0;
    for (int j = 0; j <= c; j++)
        V[0][j] = 0;
    for (int i = 1; i <= x; i++)
    {
        cout << i << "  " << n[i] << "  " << vi[i]<<"   ";

        for (int j = 1; j <= c; j++)
        {
            if (n[j] > j)
            {
                V[i][j] = V[i - 1][j];
                cout << "[" << i << "]" << "[" << j << "]" << "=" << V[i][j]<<" ";
            }

            else
            {
                V[i][j] = max(V[i - 1][j], V[i - 1][j - n[j]] + vi[i]);
                cout << "[" << i << "]" << "[" << j << "]" << "=" << V[i][j] << "   ";
            }

        }
        cout << endl;
    }

    int j = c;
    for (int i = x; i > 0; i--)
    {
        if (V[i][j] > V[i - 1][j])
        {
            choose[i] = 1;
            j = j - n[i];
        }
        else
            choose[i] = 0;
    }
    for (int i = 1; i <= x; i++)
        cout<< choose[i];
    cout << endl;

    return V[x][c];
}
int main()
{
    int c, x;
    cout << "请输入背包容量;";
    cin >> c;
    cout << "请输入物品个数:";
    cin >> x;
    int *n = new int[x+1];
    int *vi = new int[x+1];
    int *choose = new int[x+1];
    cout << "请分别输入物品重量";
    for (int i = 1; i <= x; i++)
        cin >> n[i];
    cout << "请分别输入物品价值:";
    for (int i = 1; i <= x; i++)
        cin >> vi[i];
    int k = Knapsack(n, vi, c, x,choose);
    cout << "物品最大价值为:" << k << endl;
}

VS08 测试可以编译OK

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问