zoukankan      html  css  js  c++  java
  • 背包算法

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




    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;
            }
        }
    }
  • 相关阅读:
    android loginDemo +WebService用户登录验证
    Integrating Google Sign-In into Your Android App
    Android 应用程序集成Google 登录及二次封装
    facebook 分享
    Android Facebook和Twitter登录和分享完整版
    Android 友盟社会化组件-分享实现
    Android集成友盟社会化分享功能
    c 判断是否为 字母或数字(iswalnum example)
    c 判断字符是否为字母 (iswalpha example)
    下载安装Xocde并创建一个C语言的项目工程
  • 原文地址:https://www.cnblogs.com/wewei/p/4373537.html
Copyright © 2011-2022 走看看