Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数字和目标列表->;结清总额_C#_Algorithm - Fatal编程技术网

C# 数字和目标列表->;结清总额

C# 数字和目标列表->;结清总额,c#,algorithm,C#,Algorithm,关于我从一个朋友那里得到的一个问题,我想问一个最好的解决方案 例如,我有一个整数列表 2 5 6 8 我想得到整数17 我只能用每个整数一个 在这种情况下,你能得到的最接近值是16,因为任何组合都不能得到17 public class Item { public int Weight { get; set; } public int Value { get; set; } } public class Program { public static void Main(

关于我从一个朋友那里得到的一个问题,我想问一个最好的解决方案 例如,我有一个整数列表

2 5 6 8
我想得到整数17 我只能用每个整数一个

在这种情况下,你能得到的最接近值是16,因为任何组合都不能得到17

public class Item
{
    public int Weight { get; set; }
    public int Value { get; set; }
}
public class Program
{
    public static void Main()
    {

        var items = new[]
        {
            new Item {Value = 60, Weight = 10},
            new Item {Value = 100, Weight = 20},
            new Item {Value = 120, Weight = 30},
        };

        Console.WriteLine(KnapSackRecursive(items, 50));
    }
    public static int KnapSackRecursive(Item[] items, int capacity)
    {
        // keep track of the best value seen.

        //TODO: Make it a list of numbers
        int best = 0;
        for (int i = 0; i < items.Length; i++)
        {
            // This is an array of the other items.
            var otherItems = items.Take(i).Concat(items.Skip(i + 1)).ToArray();

            // Calculate the best value without using the current item.
            int without = KnapSackRecursive(otherItems, capacity);
            int with = 0;

            // If the current item fits then calculate the best value for
            // a capacity less it's weight and with it removed from contention
            // and add the current items value to that.
            if (items[i].Weight <= capacity)
            {
                with = KnapSackRecursive(otherItems, capacity - items[i].Weight)
                    + items[i].Value;
            }

            // The current best is the max of the with or without.
            int currentBest = Math.Max(without, with);

            // determine if the current best is the overall best.
            if (currentBest > best)
                best = currentBest;
        }

        return best;
    }

}
公共类项目
{
公共整数权重{get;set;}
公共int值{get;set;}
}
公共课程
{
公共静态void Main()
{
var项目=新[]
{
新项目{Value=60,Weight=10},
新项目{Value=100,Weight=20},
新项目{Value=120,Weight=30},
};
Console.WriteLine(背包递归(项目50));
}
公共静态int背包递归(项[]项,int容量)
{
//跟踪所看到的最佳价值。
//TODO:把它列成一个数字列表
int-best=0;
对于(int i=0;i

编辑:它现在根据列表查找可能的最佳权重。这将导致发现20+30=50,因此它返回100+120=220我希望它返回(“找到最佳组合:100+120=220”),而不仅仅是(“220”)

提示:看看子集和问题。这是否回答了您的问题?相关:我进入了背包问题,并转换了类似的东西。但是背包应该一起返回值,而我想要一个独立点:看看子集和问题。这回答了你的问题吗?相关:我进入了背包问题,并转换了类似的东西。但是背包应该一起返回值,而我想要分开