C# 如何根据提供的限额进行组合?
我有一个数组,如下所示C# 如何根据提供的限额进行组合?,c#,linq,combinations,C#,Linq,Combinations,我有一个数组,如下所示 var x = new int[] { 1,2,3 }; 并且给出了一个极限作为 int limit=2; int limit=3; 我必须找到一个组合 1+2 = 3 1+3 = 4 2+3 = 5. 如果数组是 var x = new int[] { 1,2,3,4,5,6}; 并且给出了一个极限作为 int limit=2; int limit=3; 组合应该是 1+2+3 = 6 1+2+4 = 7 1+2+5 = 8 1+2+6 = 9 2
var x = new int[] { 1,2,3 };
并且给出了一个极限作为
int limit=2;
int limit=3;
我必须找到一个组合
1+2 = 3
1+3 = 4
2+3 = 5.
如果数组是
var x = new int[] { 1,2,3,4,5,6};
并且给出了一个极限作为
int limit=2;
int limit=3;
组合应该是
1+2+3 = 6
1+2+4 = 7
1+2+5 = 8
1+2+6 = 9
2+3+4 = 9
2+3+5 = 10
2+3+6 = 11
...........
............
等等
如何做到这一点
我的坏助手
var x = new int[] {1,2,3};
int limit = 2;
var m = from a1 in x
from a2 in x
select new
{
P1 = a1 ,
P2 = a2,
P3 = a1+a2
};
我会使用这个高效的组合数学项目,它也值得一读: 那么就简单到:
var x = new int[] { 1, 2, 3, 4, 5, 6 };
var combis = new Facet.Combinatorics.Combinations<int>(x, 3, Facet.Combinatorics.GenerateOption.WithoutRepetition);
foreach(var combi in combis)
Console.WriteLine(String.Join("+", combi) + "=" + combi.Sum());
Console.WriteLine("Total: " + combis.Sum(c => c.Sum())); // 201
不使用外部库的解决方案:
public static IEnumerable<IEnumerable<T>> Combinations<T>(IEnumerable<T> elements, int k)
{
return k == 0 ? new[] { new T[0] } :
elements.SelectMany((e, i) =>
Combinations(elements.Skip(i + 1),k - 1).Select(c => (new[] {e}).Concat(c)));
}
public static void Main()
{
var x = new int[] { 1, 2, 3, 4, 5, 6 };
var limit = 3;
IEnumerable<IEnumerable<int>> result = Combinations(x, limit);
foreach(var combi in result)
Console.WriteLine(String.Join("+", combi.Select(a=>a.ToString()).ToArray()) + "=" + combi.Sum());
Console.WriteLine("Total: " + result.Sum(c => c.Sum())); // 201
}
公共静态IEnumerable组合(IEnumerable元素,int k)
{
返回k==0?new[]{new T[0]}:
元素。选择多个((e,i)=>
组合(elements.Skip(i+1),k-1),选择(c=>(new[]{e}).Concat(c));
}
公共静态void Main()
{
var x=新的int[]{1,2,3,4,5,6};
var限值=3;
IEnumerable结果=组合(x,极限);
foreach(结果中的var combi)
Console.WriteLine(String.Join(“+”,combi.Select(a=>a.ToString()).ToArray())+“=”+combi.Sum());
Console.WriteLine(“总计:+result.Sum(c=>c.Sum());//201
}
编辑:所有组合:
public static IEnumerable<IEnumerable<T>> AllCombinations<T>(IEnumerable<T> elements)
{
int length = elements.Count();
for(int k = 1; k<=length; k++){
var comb = Combinations(elements, k);
foreach (IEnumerable<T> c in comb)
yield return c;
}
}
公共静态IEnumerable所有组合(IEnumerable元素)
{
int length=elements.Count();
for(intk=1;k看起来只需要一种方法
/// <summary>
/// Gets all combinations (of a given size) of a given list, either with or without reptitions.
/// </summary>
/// <typeparam name="T">The type of the elements in the list.</typeparam>
/// <param name="list">The list of which to get combinations.</param>
/// <param name="action">The action to perform on each combination of the list.</param>
/// <param name="resultSize">The number of elements in each resulting combination; or <see langword="null"/> to get
/// premutations of the same size as <paramref name="list"/>.</param>
/// <param name="withRepetition"><see langword="true"/> to get combinations with reptition of elements;
/// <see langword="false"/> to get combinations without reptition of elements.</param>
/// <exception cref="ArgumentNullException"><paramref name="list"/> is <see langword="null"/>. -or-
/// <paramref name="action"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="resultSize"/> is less than zero.</exception>
/// <remarks>
/// The algorithm performs combinations in-place. <paramref name="list"/> is however not changed.
/// </remarks>
public static void GetCombinations<T>(IList<T> list, Action<IList<T>> action, int? resultSize = null,
bool withRepetition = false)
{
if (list == null)
throw new ArgumentNullException("list");
if (action == null)
throw new ArgumentNullException("action");
if (resultSize.HasValue && resultSize.Value <= 0)
throw new ArgumentException("error", "resultSize");
var result = new T[resultSize.HasValue ? resultSize.Value : list.Count];
var indices = new int[result.Length];
for (int i = 0; i < indices.Length; i++)
indices[i] = withRepetition ? -1 : indices.Length - i - 2;
int curIndex = 0;
while (curIndex != -1)
{
indices[curIndex]++;
if (indices[curIndex] == (curIndex == 0 ? list.Count : indices[curIndex - 1] + (withRepetition ? 1 : 0)))
{
indices[curIndex] = withRepetition ? -1 : indices.Length - curIndex - 2;
curIndex--;
}
else
{
result[curIndex] = list[indices[curIndex]];
if (curIndex < indices.Length - 1)
curIndex++;
else
action(result);
}
}
}
//
///获取给定列表的所有(给定大小的)组合,可以重复,也可以不重复。
///
///列表中元素的类型。
///要获取组合的列表。
///对列表的每个组合执行的操作。
///每个结果组合中的元素数;或
///与相同大小的预置换。
///通过元素的重复获得组合;
///在不重复元素的情况下获得组合。
///是的-或者-
///是。
///小于零。
///
///该算法在适当的位置执行组合,但不会更改。
///
公共静态void getcompositions(IList列表,Action-Action,int?resultSize=null,
bool with repeation=false)
{
if(list==null)
抛出新的ArgumentNullException(“列表”);
if(action==null)
抛出新的异常(“操作”);
if(resultSize.HasValue&&resultSize.Value组合result.Add(list.Sum())、limit、false);
代码取自您必须使用linq吗?是否严格需要linq?如果我们使用linq并且没有外部库/代码,会更好没有其他简单的方法..不使用它library@priyanka.sarkar库出了什么问题?创建我们自己的方法的开销将非常大,它可以工作到相同的测试级别large@priyanka.sarkar请看我的答案-它提供了所有需要的代码。您可以避免任何外部库。+1即使允许我仍然使用库:)是的,因为C(1000,3)
(result.Count()
)基本上是无限的(1.66167e+8
)编辑:你删除了/编辑了你的评论!!1000个元素对于提取每一个可能的3个数字组合来说都是一个非常大的数字!不,有限,166167000,因为库的产量为20毫秒;)@Ahmed KRAIEM,我如何修改程序以获得所有的组合,即不考虑任何限制。如果你能将它与现有的组合一起附加,那就太好了