当前位置:   article > 正文

实验三:背包问题(贪心)_给定n(n<=10)种物品和一个背包。物品i的重量是wi,价值为vi,背包的容量为c(c<=1000

给定n(n<=10)种物品和一个背包。物品i的重量是wi,价值为vi,背包的容量为c(c<=1000

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;//输出装入物品的总价值
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

5.运行结果

在这里插入图片描述

6.实验分析

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/401584
推荐阅读
相关标签
  

闽ICP备14008679号