我想睡觉


私信TA

用户名:beiqian

访问量:4087

签 名:

等  级
排  名 1645
经  验 2632
参赛次数 0
文章发表 18
年  龄 0
在职情况 学生
学  校 武汉理工大学
专  业

  自我简介:

视频讲解:https://www.bilibili.com/video/BV1Lh411n7wi/

参考代码:

#include #include #include #include using namespace std;
int n,m;//n表示物品数量,m表示背包容量
struct stuff
{
    int w,v;//w表示重量,v表示价值
};
stuff s[5050];

int dp[5050][5050];

int f(int i,int j)//用来获取dp[i][j]
{
    if(i<1||j=s[i].w)
            dp[i][j] = dp[i][j] = max(f(i-1,j),f(i-1,j-s[i].w)+s[i].v);
        else dp[i][j] = f(i-1,j);
    }
    return dp[i][j];
}

int main()
{
    //freopen("in.txt","r",stdin);
    cin >> n >> m;
    for(int i = 1; i > s[i].w >> s[i].v;
    //输入完毕
    /*
    cout << "输出dp表"<<endl;
    for(int i = 0; i <=n; ++i)
    {
        for(int j = 0; j <= m; ++j)
        {
            cout << setw(2) << f(i,j) << " ";
        }
        cout << "\n";
    }*/
    cout << f(n,m) << endl;
    return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区