C# 获取数组中的所有组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1272828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting all the combinations in an array
提问by nfplee
Say I have the following array:
假设我有以下数组:
var arr = new[] { "A", "B", "C" };
How can I produce all the possible combinations that contain only two characters and no two the same (e.g. AB
would be the same as BA
). For example, using the above array it would produce:
如何生成仅包含两个字符且没有两个相同的所有可能组合(例如,AB
将与 相同BA
)。例如,使用上面的数组它会产生:
AB
AC
BC
Please note that this example has been simplified. The array and the length of the string required will be greater.
请注意,此示例已被简化。所需的数组和字符串的长度会更大。
I'd really appreciate if someone could help.
如果有人可以提供帮助,我将不胜感激。
采纳答案by Victor Hurdugaci
回答by Arjan Einbu
Lets extend it, so maybe we can see the pattern:
让我们扩展它,所以也许我们可以看到模式:
string[] arr = new string[] { "A", "B", "C", "D", "E" };
//arr[0] + arr[1] = AB
//arr[0] + arr[2] = AC
//arr[0] + arr[3] = AD
//arr[0] + arr[4] = AE
//arr[1] + arr[2] = BC
//arr[1] + arr[3] = BD
//arr[1] + arr[4] = BE
//arr[2] + arr[3] = CD
//arr[2] + arr[4] = CE
//arr[3] + arr[4] = DE
I see two loops here.
我在这里看到两个循环。
- The first (outer) loop goes from 0 to 4 (arr.Length - 1)
- The second (inner) loop goes from the outer loops counter + 1 to 4 (arr.Length)
- 第一个(外部)循环从 0 到 4 (arr.Length - 1)
- 第二个(内)循环从外循环计数器 + 1 到 4 (arr.Length)
Now it should be easy to translate that to code!
现在应该很容易将其转换为代码!
回答by Matt J
回答by user152108
What you are looking for is an double Loop along the lines of the following pseudo code.
您正在寻找的是沿着以下伪代码行的双循环。
for(int i = FirstElement; i<= LastElement; increment i) {
for(j = i; j<= lastElement; increment j) {
if(i != j) {
print (i, j)
}
}
}
回答by JSB????
public string[] Permute(char[] characters)
{
List<string> strings = new List<string>();
for (int i = 0; i < characters.Length; i++)
{
for (int j = i + 1; j < characters.Length; j++)
{
strings.Add(new String(new char[] { characters[i], characters[j] }));
}
}
return strings.ToArray();
}
回答by stevedbrown
It's the sum of 1 to n-1 or n(n-1) / 2.
它是 1 到 n-1 或 n(n-1) / 2 的总和。
int num = n * ( n - 1 ) / 2;
Obviously you could generalize the n * ( n - 1 ) using a pair of factorials for whatever you are trying to do (string size wise).
显然,您可以使用一对阶乘对 n * ( n - 1 ) 进行概括,无论您要做什么(字符串大小明智)。
回答by Alex Martelli
What you're asking for are combinations, not permutations (the latter term implies that order matters). Anyway, it's a classic use for recursion. In pseudo-code:
您要求的是组合,而不是排列(后一个术语意味着顺序很重要)。无论如何,这是递归的经典用法。在伪代码中:
def combs(thearray, arraylen, currentindex, comblen):
# none if there aren't at least comblen items left,
# or comblen has gone <= 0
if comblen > arraylen - currentindex or comblen <= 0:
return
# just 1 if there exactly comblen items left
if comblen == arraylen - currentindex:
yield thearray[currentindex:]
return
# else, all combs with the current item...:
for acomb in combs(thearray, arraylen, currentindex+1, comblen-1):
yield thearray[currentindex] + acomb
# ...plus all combs without it:
for acomb in combs(thearray, arraylen, currentindex+1, comblen):
yield acomb
回答by Kamarey
Didn't tested and not the fastest, but:
没有测试,也不是最快的,但是:
IEnumerable<String> Combine(String text, IEnumerable<String> strings)
{
return strings.Select(s => text + s).Concat(Combine(strins.Take(1).First(), strings.Skip(1))
}
Call:
称呼:
foreach (var s in Combine("" , arrayOfStrings))
{
// print s
}
回答by Adam Kewley
Wrote an answer for a question that turned out to be marked as duplicate, pointing here.
为一个结果被标记为重复的问题写了一个答案,指向这里。
var arr = new[] { "A", "B", "C" };
var arr2 = arr1.SelectMany(
x => arr1.Select(
y => x + y));
Produced the correct output when enumerated into console in VS2013. SelectMany
will flatten the internal IEnumerable
generated from the inner Select
.
在 VS2013 中枚举到控制台时生成正确的输出。SelectMany
将展平从内部IEnumerable
生成的内部Select
。