C# 如何获取arraylist的所有组合?
我有一个字符串数组列表“abcde” 我想要一个方法返回另一个arraylist,其中包含C中给定arraylist(例如ab、ac、ad…)的所有可能组合# 有人知道一个简单的方法吗C# 如何获取arraylist的所有组合?,c#,arraylist,combinations,C#,Arraylist,Combinations,我有一个字符串数组列表“abcde” 我想要一个方法返回另一个arraylist,其中包含C中给定arraylist(例如ab、ac、ad…)的所有可能组合# 有人知道一个简单的方法吗 注意:长度2的所有可能组合,如果长度是可变的(可以更改),则会更好。关于需要长度2组合的注释: string s = "abcde"; var combinations = from c in s from d in s.Remove(s.IndexOf(c), 1)
注意:长度2的所有可能组合,如果长度是可变的(可以更改),则会更好。关于需要长度2组合的注释:
string s = "abcde";
var combinations = from c in s
from d in s.Remove(s.IndexOf(c), 1)
select new string(new[] { c, d });
foreach (var combination in combinations) {
Console.WriteLine(combination);
}
响应任意长度的编辑:
static IEnumerable<string> GetCombinations(string s, int length) {
Guard.Against<ArgumentNullException>(s == null);
if (length > s.Length || length == 0) {
return new[] { String.Empty };
if (length == 1) {
return s.Select(c => new string(new[] { c }));
}
return from c in s
from combination in GetCombinations(
s.Remove(s.IndexOf(c), 1),
length - 1
)
select c + combination;
}
输出:
abc, abd, abe, acb, acd, ace, adb, adc, ade, aeb, aec, aed, bac, bad, bae, bca,
bcd, bce, bda, bdc, bde, bea, bec, bed, cab, cad, cae, cba, cbd, cbe, cda, cdb,
cde, cea, ceb, ced, dab, dac, dae, dba, dbc, dbe, dca, dcb, dce, dea, deb, dec,
eab, eac, ead, eba, ebc, ebd, eca, ecb, ecd, eda, edb, edc
aa, ab, ac, ad, ae, ba, bb, bc, bd, be, ca, cb, cc, cd, ce, da, db, dc, dd, de, ea, eb, ec, ed, ee
下面是我的泛型函数,它可以返回T类型的所有组合:
static IEnumerable<IEnumerable<T>> GetCombinations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetCombinations(list, length - 1)
.SelectMany(t => list, (t1, t2) => t1.Concat(new T[] { t2 }));
}
输出:
abc, abd, abe, acb, acd, ace, adb, adc, ade, aeb, aec, aed, bac, bad, bae, bca,
bcd, bce, bda, bdc, bde, bea, bec, bed, cab, cad, cae, cba, cbd, cbe, cda, cdb,
cde, cea, ceb, ced, dab, dac, dae, dba, dbc, dbe, dca, dcb, dce, dea, deb, dec,
eab, eac, ead, eba, ebc, ebd, eca, ecb, ecd, eda, edb, edc
aa, ab, ac, ad, ae, ba, bb, bc, bd, be, ca, cb, cc, cd, ce, da, db, dc, dd, de, ea, eb, ec, ed, ee
已更新
请参阅我对其他场景的回答,例如排列和k组合等。仅使用数组和递归组合数组中的数字:
static int n = 4;
int[] baseArr = { 1, 2, 3, 4 };
int[] LockNums;
static void Main(string[] args)
{
int len = baseArr.Length;
LockNums = new int[n];
for (int i = 0; i < n; i++)
{
int num = baseArr[i];
DoCombinations(num, baseArr, len);
//for more than 4 numbers the print screen is too long if we need to check the result next line will help
//Console.ReadLine();
}
}
private void DoCombinations(int lockNum, int[] arr, int arrLen )
{
int n1 = arr.Length;
// next line shows the difference in length between the previous and its previous array
int point = arrLen - n1;
LockNums[n - arr.Length] = lockNum;
int[] tempArr = new int[arr.Length - 1];
FillTempArr(lockNum, arr, tempArr);
//next condition will print the last number from the current combination
if (arr.Length == 1)
{
Console.Write(" {0}", lockNum);
Console.WriteLine();
}
for (int i = 0; i < tempArr.Length; i++)
{
if ((point == 1) && (i != 0))
{
//without this code the program will fail to print the leading number of the next combination
//and 'point' is the exact moment when this code has to be executed
PrintFirstNums(baseArr.Length - n1);
}
Console.Write(" {0}", lockNum);
int num1 = tempArr[i];
DoCombinations(num1, tempArr, n1);
}
}
private void PrintFirstNums(int missNums)
{
for (int i = 0; i < missNums; i++)
{
Console.Write(" {0}", LockNums[i]);
}
}
private void FillTempArr(int lockN, int[] arr, int[] tempArr)
{
int idx = 0;
foreach (int number in arr)
{
if (number != lockN)
{
tempArr[idx++] = number;
}
}
}
private void PrintResult(int[] arr)
{
foreach (int num in arr)
{
Console.Write(" {0}", num);
}
}
static int n=4;
int[]baseArr={1,2,3,4};
int[]LockNums;
静态void Main(字符串[]参数)
{
int len=基准长度;
LockNums=新整数[n];
对于(int i=0;i
您的意思是所有长度组合=2吗?所有可能的长度组合2除非您的目标是.NET 1.1,否则您应该使用列表。ArrayList已弃用。正在使用windows窗体application@Tergiver:否,ArrayList
类未被弃用。它实际上已经过时了。您还应该执行var retComb=getcombines(s,2).Distinct()。其中(p=>p[0]>=p[p.Length]);要删除多余的项,并反转项,例如aabd
测试您的方法是重复排列而不是组合排列@HalilKaskavalci,请查看更多选项。现在看起来好多了。您好,这是我的数字组合解决方案。该解决方案仅使用阵列。我之所以写这篇文章,是因为我在任何地方都没有遇到使用阵列的解决方案。该代码是为快速演示而设置的,否则它应该适用于任何数组长度。