我有一个随机值列表,如下所示
319 4, 90, 50, 20, 99, 500, 95, 900
我必须找到一个总和在所选范围内的值,例如5%到10%。
例如,如果数字是300 范围为5%至10%
那么差异应该在15到30的范围内
然后满足这个条件的列表是
319 => 319-300 = -19,最接近300,差异在5%到10%之间 319,4 => 319 + 4 = 323 => 323-300 = -23,最接近300,差异在5%到10%之间 90,99,97 => 90 + 99 + 95 = 284 => 284-300 = 16,最接近300,差异在5%到10%范围内
结果将是
319,
319,4
90,99,95
我尝试过修改递归算法 (Efficient algorithm to find a combination, which summation is equal to a known number, in a set of number) 但它只能返回几个匹配的序列而不是全部。
代码:
public static IEnumerable<string> GetSequence(decimal[] set, decimal? sum, decimal? startPercent, decimal? endPercent, string values = "")
{
for (int i = 0; i < set.Length; i++)
{
decimal? left = sum - set[i];
string vals = set[i] + "," + values;
if (Math.Abs(decimal.Parse(left.ToString())) >= startPercent && Math.Abs(decimal.Parse(left.ToString())) <= endPercent)
{
yield return vals;
}
else
{
decimal[] possible = set.Take(i).Where(n => n <= sum).ToArray();
if (possible.Length > 0)
{
foreach (string s in GetSequence(possible, left, startPercent, endPercent, vals))
{
yield return s;
}
}
}
}
}
有人可以帮我解决这个问题。
答案 0 :(得分:3)
可能更好的方法是使用如下代码生成所有可能的组合:
public static IEnumerable<IEnumerable<T>> Combinations<T>(IList<T> items)
{
return Combinations(items.Count).Select(comb => comb.Select(index => items[index]));
}
public static IEnumerable<IEnumerable<int>> Combinations(int n)
{
long m = 1 << n;
for (long i = 1; i < m; ++i)
yield return bitIndices((uint)i);
}
static IEnumerable<int> bitIndices(uint n)
{
uint mask = 1;
for (int bit = 0; bit < 32; ++bit, mask <<= 1)
if ((n & mask) != 0)
yield return bit;
}
然后你可以编写一种方法来总结每种可能的组合:
static IEnumerable<(int Sum, List<int> Values)> SummedCombinations(IList<int> values)
{
return
Combinations(values)
.Select(comb => comb.ToList())
.Select(comb => (comb.Sum(), comb));
}
然后你可以编写一个方法来查找总和与你正在寻找的范围相匹配的所有组合:
static IEnumerable<List<int>> FindMatches(IList<int> values, int target, int toleranceLow, int toleranceHigh)
{
int minDiff = (target * toleranceLow) / 100;
int maxDiff = (target * toleranceHigh) / 100;
foreach (var sum in SummedCombinations(values))
{
int diff = Math.Abs(sum.Sum - target);
if (minDiff <= diff && diff <= maxDiff)
yield return sum.Values;
}
}
将所有内容整合到一个可编辑的控制台应用程序中:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
int[] values = {319, 4, 90, 50, 20, 99, 500, 95, 900};
foreach (var combination in FindMatches(values, 300, 5, 10))
{
Console.WriteLine(string.Join(", ", combination));
}
}
static IEnumerable<List<int>> FindMatches(IList<int> values, int target, int toleranceLow, int toleranceHigh)
{
int minDiff = (target * toleranceLow) / 100;
int maxDiff = (target * toleranceHigh) / 100;
foreach (var sum in SummedCombinations(values))
{
int diff = Math.Abs(sum.Sum - target);
if (minDiff <= diff && diff <= maxDiff)
yield return sum.Values;
}
}
static IEnumerable<(int Sum, List<int> Values)> SummedCombinations(IList<int> values)
{
return
Combinations(values)
.Select(comb => comb.ToList())
.Select(comb => (comb.Sum(), comb));
}
public static IEnumerable<IEnumerable<T>> Combinations<T>(IList<T> items)
{
return Combinations(items.Count).Select(comb => comb.Select(index => items[index]));
}
public static IEnumerable<IEnumerable<int>> Combinations(int n)
{
long m = 1 << n;
for (long i = 1; i < m; ++i)
yield return bitIndices((uint)i);
}
static IEnumerable<int> bitIndices(uint n)
{
uint mask = 1;
for (int bit = 0; bit < 32; ++bit, mask <<= 1)
if ((n & mask) != 0)
yield return bit;
}
}
}
输出:
319
319, 4
90, 99, 95
您的预期输出是什么。
注意:上面的代码使用的是C#7元组 - 如果您使用的是早期版本,则必须将FindMatches()
和SummedCombinations()
更改为:
static IEnumerable<List<int>> FindMatches(IList<int> values, int target, int toleranceLow, int toleranceHigh)
{
int minDiff = (target * toleranceLow) / 100;
int maxDiff = (target * toleranceHigh) / 100;
foreach (var sum in SummedCombinations(values))
{
int diff = Math.Abs(sum.Item1 - target);
if (minDiff <= diff && diff <= maxDiff)
yield return sum.Item2;
}
}
static IEnumerable<Tuple<int, List<int>>> SummedCombinations(IList<int> values)
{
return
Combinations(values)
.Select(comb => comb.ToList())
.Select(comb => Tuple.Create(comb.Sum(), comb));
}
组合部分的说明
组合的工作原理如下:
i
,其中N是要合并的项目数。i
中设置的每个位,返回输入值中相应位置的项目。例如,如果你有3个值; A,B和C:
i
将从1到(2 ^ 3-1)= 7。
查看我们将获得的1..7的二进制值,并查看A,B,C输入的相应元素:
C B A (Input)
2 1 0 (Bit number, i.e. power of two)
---------------------------------------
0 0 1 [1] = A
0 1 0 [2] = B
0 1 1 [3] = A B
1 0 0 [4] = C
1 0 1 [5] = A C
1 1 0 [5] = B C
1 1 1 [6] = A B C