gpt4 book ai didi

c# - 如何在列表中查找总和小于或等于数字的项目

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:21 24 4
gpt4 key购买 nike

我有一个数字列表,我想知道列表中数字的哪个组合具有最接近特定数字(目标)的总和。(如果有很多组合,找到一个就足够了)

我搜索了很多,知道这里有很多解决方案可以找到它们的总和等于特定数字的组合,但是没有找到最接近该数字的解决方案。

我还写了一段代码,从零循环到“目标”来找到最大的总和,但由于这个过程非常耗时,因为它必须计算 100,000 个列表,我想知道是否有更有效的方法使用最好是 linq。

var List1 = new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45 };
var target = 40;

int MaxViewers = Convert.ToInt32(txtNoRecordsToAdd.Text);

for (int UserNo = 1; UserNo <= MaxViewers; UserNo++)
{
for (int No = 1; No <= target; No++)
{
var matches = from subset in MyExtensions.SubSetsOf(List1)
where subset.Sum() == target
select subset;
}
}

public static class MyExtensions
{
public static IEnumerable<IEnumerable<T>> SubSetsOf<T>(this IEnumerable<T> source)
{
if (!source.Any())
return Enumerable.Repeat(Enumerable.Empty<T>(), 1);

// Grab the first element off of the list
var element = source.Take(1);

// Recurse, to get all subsets of the source, ignoring the first item
var haveNots = SubSetsOf(source.Skip(1));

// Get all those subsets and add the element we removed to them
var haves = haveNots.Select(set => element.Concat(set));

// Finally combine the subsets that didn't include the first item, with those that did.
return haves.Concat(haveNots);
}

}

最佳答案

你好,让我们检查一个棘手的方法来实现这一点,

var List1 = new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45 };
var target = 40;

int MaxViewers = Convert.ToInt32(txtNoRecordsToAdd.Text);

var closestSubSet = MyExtensions.SubSetsOf(List1)
.Select(o=> new{ SubSet = o, Sum = o.Sum(x=> x)})
.Select(o=> new{ SubSet = o.SubSet, Sum = o.Sum, FromTarget = (target - o.Sum >= 0 ? target - o.Sum : (target - o.Sum) * -1 ) })
.OrderBy(o=> o.FromTarget).FirstOrDefault();

我知道出于某些性能原因我做出第二次选择很棘手(不是多次调用求和而是使用它)。这应该找到与您指定的目标最接近的总和 ^^ 玩得开心


优化:

var List1 = new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45 }; var 目标 = 40;

int MaxViewers = Convert.ToInt32(txtNoRecordsToAdd.Text);
int minClosestTargetRequired = 0;
int closestSum = int.maxValue;
int closestSetIndex = -1;
var subsets = MyExtensions.SubSetsOf(List1);
for(var c = 0 ; c < subsets.Count() ; c++)
{
int currentSum = subsets[c].Sum(o=> o);
if(currentSum < closestSum)
{
closestSum = currentSum;
closestSetIndex = c;
}
if(closestSum <= minClosestTargetRequired)
break;
}
Console.WriteLine("Closest Sum Is {0} In Set Index {1},closestSum,closestSetIndex);

关于c# - 如何在列表中查找总和小于或等于数字的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com