数据结构(C#)-- 贪心算法解决背包问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data ;
using System.Collections ;
namespace Kapbag
{
    public class Knapsack
    {
        private float quantity;
        SortedList items = new SortedList();
        string itemList;
        public Knapsack(float max)
        {
            quantity = max;
        }
        public void FillSack(ArrayList objects)
        {
            int pos = objects.Count - 1;
            int totalUnits = 0;
            float totalVal = 0.0F;
            int tempTot = 0;
            while (totalUnits < quantity)
            {
                tempTot += ((Carpet)objects[pos]).GetUnit();
                if (tempTot <= quantity)
                {
                    totalUnits += ((Carpet)objects[pos]).GetUnit();
                    totalVal += ((Carpet)objects[pos]).GetVal();
                    items.Add(((Carpet)objects[pos]).GetItem(), ((Carpet)objects[pos]).GetUnit());
                }
                else
                {
                    float tempUnit = quantity - totalUnits;
                    float tempVal = ((Carpet)objects[pos]).ItemVal() * tempUnit;
                    totalVal += tempVal;
                    totalUnits += (int)tempUnit;
                    items.Add(((Carpet)objects[pos]).GetItem(), tempUnit);
                }
                pos--;
            }
        }
        public string GetItems()
        {
            foreach (Object k in items.GetKeyList())
                itemList += k.ToString() + ": " + items[k].
                ToString() + " ";
            return itemList;
        }
        static void Main()
        {
            Carpet c1 = new Carpet("Frieze", 1.75F, 12);
            Carpet c2 = new Carpet("Saxony", 1.82F, 9);
            Carpet c3 = new Carpet("Shag", 1.5F, 13);
            Carpet c4 = new Carpet("Loop", 1.77F, 10);
            ArrayList rugs = new ArrayList();
            rugs.Add(c1);
            rugs.Add(c2);
            rugs.Add(c3);
            rugs.Add(c4);
            rugs.Sort();
            Knapsack k = new Knapsack(25);
            k.FillSack(rugs);
            Console.WriteLine(k.GetItems());
        }
    }


    public class Carpet : IComparable
    {
        private string item;
        private float val;
        private int unit;
        public Carpet(string i, float v, int u)
        {
            item = i;
            val = v;
            unit = u;
        }
        public int CompareTo(Object c)
        {
            return (this.val.CompareTo(((Carpet)c).val));
        }
        public int GetUnit()
        {
            return unit;
        }
        public string GetItem()
        {
            return item;
        }
        public float GetVal()
        {
            return val * unit;
        }
        public float ItemVal()
        {
            return val;
        }
    }
 

}

截图:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值