01背包问题动态规划解法(c语言)

int max(int a,int b)
{
    if(a>b) return a;
    else return b;
}
/**
 * [getAns 求解方法]
 * @param  s [背包容量]
 * @param  n [物品个数]
 * @param  w [存放物品重量的数组]
 * @param  v [存放物品价值的数组]
 * @return   [description]
 */
int getAns(int s,int n,int w[],int v[])
{
    int i,j;
    int ans[n +1][s + 1];
    //初始化第一个物品
    for(j=0;j<=s;j++) ans[1][j]=w[1]<=j?v[1]:0;
    for(i=2;i<=n;i++)
    {
        int jmin=w[i]<s?w[i]:s;
        //物品重量大于容量的情况
        for(j=0;j<jmin;j++) ans[i][j]=ans[i-1][j];
        //物品重量小于容量的情况
        for(j=w[i];j<=s;j++){
        	ans[i][j]=max(ans[i-1][j],ans[i-1][j-w[i]]+v[i]);
        }
    }
    return ans[n][s];
}
int main()
{
    int i,answer;
    int s;
    int n;
    printf("Please enter the capacity of the backpack and the number of items:\n");
    scanf("%d %d",&s,&n);
    int w[n+1];
    int v[n+1];
    printf("Please enter the weight and value of the item in turn:\n");
    for(i=1;i<=n;i++) scanf("%d %d",&w[i],&v[i]);
    answer=getAns(s,n,w,v);
    printf("Maximum value:%d\n",answer);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值