背包算法

程序实现功能: 有几种重量不同的石头,现在需要从这几种石头中搬指定重量的石头,随意组合,如何才能搬的个数最少。(一定要搬这个重量的石头,不能多也不能少)。如何石头的组合不能得到这个重量就可以不管




using System;
using System.Collections.Generic;
using System.Text;

namespace beibao
{
    class Program
    {
        static void Main(string[] args)
        {
            int targetWeight = 1342;
            List<int> typeList = new List<int>();
            typeList.Add(100);
            typeList.Add(70);
            typeList.Add(40);
            typeList.Add(10);
            typeList.Add(8);
            typeList.Sort();
            typeList.Reverse();
            Stack<BeiBaoResult> result = GetBeiBaoResult(targetWeight,typeList);
            
            foreach (BeiBaoResult item in result)
            {
                Console.WriteLine(item.TypeWeight + ":" + item.TypeCount + "");
            }

        }

        public static Stack<BeiBaoResult> GetBeiBaoResult(int targetWeight,List<int> typeList)
        {
            Stack<BeiBaoResult> result = new Stack<BeiBaoResult>();

            for (int i = 0; i < typeList.Count; i++)
            {
                int remainWeight; //剩余重量
                if (i == 0)
                {
                    remainWeight = targetWeight;
                }
                else
                {
                    remainWeight = result.Peek().RemainWeight;
                }

                BeiBaoResult beiBaoResult = new BeiBaoResult();
                beiBaoResult.TypeWeight = typeList[i];
                beiBaoResult.TypeCount = remainWeight / typeList[i];
                beiBaoResult.RemainWeight = remainWeight % typeList[i];
                result.Push(beiBaoResult);
                if (beiBaoResult.RemainWeight == 0)
                {
                    return result;
                }             
            }

            while (result.Count>1)
            {
                result.Pop();
                BeiBaoResult lastBeiBaoItem = result.Peek();

                while (lastBeiBaoItem.TypeCount > 0)
                {
                    lastBeiBaoItem.TypeCount = lastBeiBaoItem.TypeCount - 1;
                    lastBeiBaoItem.RemainWeight = lastBeiBaoItem.RemainWeight + lastBeiBaoItem.TypeWeight;
                   
                    List<int> typeList2 = new List<int>();
                    typeList2 = typeList.GetRange(result.Count, typeList.Count - result.Count);

                    Stack<BeiBaoResult> beiBaoResult2 = GetBeiBaoResult(lastBeiBaoItem.RemainWeight, typeList2);

                    if (beiBaoResult2.Peek().RemainWeight == 0)
                    {
                        while (beiBaoResult2.Count > 0)
                        {
                            result.Push(beiBaoResult2.Pop());
                        }
                        return result;
                    }
                }                                

            }

            return result;
        }


        /// <summary>
        /// 背包结果项
        /// </summary>
        public class BeiBaoResult
        {
            //种类重量
            public int TypeWeight;
            
            //种类个数
            public int TypeCount;
            
            
            //剩余重量
            public int RemainWeight;
        }
    }
}

 

posted @ 2015-03-28 04:20  wenwei  阅读(244)  评论(0编辑  收藏  举报