实验三:背包问题(贪心)

1.实验题目

背包问题
给定n种物品和一个背包。物品i的重量是Wi,其价值为Vi,背包的容量为C。应如何选择装入背包的物品,使得装入背包中物品的总价值最大?

2.实验目的

  1. 掌握贪心算法的基本思想;
  2. 掌握贪心算法中贪心选择性质的分析与证明;
  3. 掌握贪心算法求解问题的方法。

3.实验要求

(1)根据实验内容构思设计算法,选取适合的数据结构; 
(2)对所设计的算法采用大O符号进行时间复杂性分析; 
(3)上机实现算法,编程语言不限; 
(4)实验报告内容应包括问题描述、问题分析、算法设计、算法实现、运行结果及算法复杂度分析等内容。

4.实验过程

#include <iostream>
#include <algorithm>
#define MAX_NUM 1000
using namespace std;
 
struct Goods //info of goods定义物品信息结构体
{
    int weight;// the weight of goods重量
    int value;// the value of goods价值
    double ValPerWei;// value per weight权重
    double load; //物品装入背包的部分的系数(例如上图中物品全部装入则load为1,装入10/20则load为0.5)
};
int cmp( Goods const &a, Goods const &b)//定义sort函数的比较函数
{
    if(a.ValPerWei<b.ValPerWei) return 0;
    else return 1;
}
void Greedy(Goods g[],int good_num, int content)//贪心算法
{
    for(int i=0; i<good_num; i++)
    {
        if(content>g[i].weight)//如果背包足够装下整个物品
        {
            content-=g[i].weight;
            g[i].load=1;
        }
        else if(content>0){//如果背包不足以装下整个物品
            g[i].load=(double)content/g[i].weight;//计算物品装入背包的部分
            content=0;//背包容量置0
            return;//程序结束,返回
        }
    }
}
int main()
{
    int goods_num;
    int bagvol;
    double total_value=0;
    double total_weight=0;
    cout<<"Please input the volum of bag:"<<endl;
    cin>>bagvol;
    cout<<"Please input the number of goods:"<<endl;
    cin>>goods_num;
    Goods G[goods_num+1];
    cout<<"Please input the weight and value of goods:"<<endl;
    for(int i=0; i<goods_num; i++)
    {
        cin>>G[i].weight>>G[i].value;//输入重量价值
        G[i].ValPerWei=(double)G[i].value/G[i].weight;//计算权重
        G[i].load=0;//load置0
    }
 
    sort (G,G+goods_num,cmp);//sort by ValPerWei
 
    Greedy(G,goods_num,bagvol);
    cout<<"Info of loaded goods:"<<endl;
    for(int i=0;i<goods_num;i++)//output the info of goods
    {
        if(G[i].load==0.0)break;//如果检测到物品未被装入背包,则推出循环
 
        total_value+=(G[i].value*G[i].load);//装入背包的物品总价值
        total_weight+=(G[i].weight*G[i].load);//装入背包的物品总重量
        cout<<"weight: "<<G[i].weight<<"  "<<"value: "<<G[i].value<<"  "<<"the value per weight of good: "<<G[i].ValPerWei<<"  the part of goods: "<<G[i].load<<endl;//输出装入背包的物品信息
    }
    cout<<"the volum of bag is: "<<bagvol<<endl;//输出背包容量
    cout<<"the total weight is: "<<total_weight<<endl;//输出装入物品的总重量
    cout<<"the total value is: "<<total_value<<endl;//输出装入物品的总价值
}

5.运行结果

在这里插入图片描述

6.实验分析

通过这次实验,我较为透彻的理解了贪心算法的基本思想和几个基本步骤,而且进一步提高了自己的自学能力和编程能力。在求解背包问题中,关键是三种贪心策略的设计。贪心算法求解问题一般具有两个重要性质:贪心选择性质和最优子结构性质。虽然贪心算法能尽可能快的求得更好的解,但也存在一些局限性,例如不能保证求得最终的解是最佳的,不能用来求最大最小解问题,只能满足某些约束条件的可行解的范围。

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值