杭电2602 01背包算法的讲解与具体实现

背包的最简单的实践题目
Bone Collector
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23125    Accepted Submission(s): 9387




Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?


 


Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
 


Output
One integer per line representing the maximum of the total value (this number will be less than 231).
 


Sample Input


1 5 10 1 2 3 4 5 5 4 3 2 1


 


Sample Output


14

下边是代码实现

#include <iostream>

#include <cstdio>
#include <cstring>
using namespace std;


int main()
{
    int a[1010],f[1010];
    int b[1010],n,m,k,i,j;
    scanf("%d",&k);
    while(k--)
    {
        memset(a,0,sizeof(a));//清零
        memset(f,0,sizeof(f));
        memset(b,0,sizeof(b));
        scanf("%d %d",&n,&m);
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=0;i<n;i++)
            scanf("%d",&b[i]);
            for(i=0;i<n;i++)//01背包的具体实现过程
            {
                for(j=m;j>=b[i];--j)
            {
                if(f[j]<f[j-b[i]]+a[i])
                {
                    f[j]=f[j-b[i]]+a[i];
                }


            }
            }
            printf("%d\n",f[m]);




    }
    return 0;
}
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
01背包的具体实现过程及代码
#include<iostream>
#define MAX 1111
using namespace std;
    int f[MAX],n,m,v,w;
int main(){
    cin>>n>>m;//n表示个数,m表示背包容量
    for(int i=1;i<=n;++i){
        cin>>v>>w;//v=价值,w=重量
        for(int j=m;j>=w;--j)
            if(f[j]<f[j-w]+v)
            {
                 f[j]=f[j-w]+v;
                    cout<<f[j];
                 cout<<"\t";
                 }
    }
    cout<<f[m]<<'\n';
    return 0;
}
/**
5 12
1 2
4 5
5 6
7 8
5 2
**/
背包的过程实现如下
12   11   10   9   8   7   6   5   4   3   2   1   0
1    1     1   1   1   1   1   1   1   1   1   0   0   (1,2)
5    5     5   5   5   5   4   4                        (4,5)
9    9     6   6   6   5   5                            (5,6)//倒数第二个5不更新
8    8     8   7   7                                     (7,8)//前两个8不更新
13   12    12  10  10  9   6   6   6   5   5             (5,2)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
社会发展日新月异,用计算机应用实现数据管理功能已经算是很完善的了,但是随着移动互联网的到来,处理信息不再受制于地理位置的限制,处理信息及时高效,备受人们的喜爱。所以各大互联网厂商都瞄准移动互联网这个潮流进行各大布局,经过多年的大浪淘沙,各种移动操作系统的不断面世,而目前市场占有率最高的就是微信小程序,本次开发一套基于微信小程序的生签到系统,有管理员,教师,学生三个角色。管理员功能有个人中心,学生管理,教师管理,签到管理,学生签到管理,班课信息管理,加入班课管理,请假信息管理,审批信息管理,销假信息管理,系统管理。教师和学生都可以在微信端注册和登录,教师可以管理签到信息,管理班课信息,审批请假信息,查看学生签到,查看加入班级,查看审批信息和销假信息。学生可以查看教师发布的学生签到信息,可以自己选择加入班课信息,添加请假信息,查看审批信息,进行销假操作。基于微信小程序的生签到系统服务端用Java开发的网站后台,接收并且处理微信小程序端传入的json数据,数据库用到了MySQL数据库作为数据的存储。这样就让用户用着方便快捷,都通过同一个后台进行业务处理,而后台又可以根据并发量做好部署,用硬件和软件进行协作,满足于数据的交互式处理,让用户的数据存储更安全,得到数据更方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值