C# 查找数组中所有项目组合的最佳方法是什么?
在c#中查找数组中所有项目组合的最佳方法是什么?它是O(n!)C# 查找数组中所有项目组合的最佳方法是什么?,c#,algorithm,C#,Algorithm,在c#中查找数组中所有项目组合的最佳方法是什么?它是O(n!) 静态列表梳; 使用静态bool[]; 静态void GetCombinationSample() { int[]arr={10,50,3,1,2}; 已使用=新布尔值[arr.Length]; 使用。填写(假); comb=新列表(); 列表c=新列表(); GetComb(arr,0,c); foreach(组合中的var项目) { foreach(项目中的变量x) { 控制台。写(x+“,”); } 控制台。写线(“”); }
静态列表梳;
使用静态bool[];
静态void GetCombinationSample()
{
int[]arr={10,50,3,1,2};
已使用=新布尔值[arr.Length];
使用。填写(假);
comb=新列表();
列表c=新列表();
GetComb(arr,0,c);
foreach(组合中的var项目)
{
foreach(项目中的变量x)
{
控制台。写(x+“,”);
}
控制台。写线(“”);
}
}
静态void GetComb(int[]arr,int colindex,列表c)
{
如果(colindex>=阵列长度)
{
增加(新名单(c));
返回;
}
对于(int i=0;i
也许可以提供一些帮助(参见主页上的示例):
KwCombinatorics库有3个类,提供3种不同的方法来生成数字组合的有序(排序)列表。这些组合法对于软件测试非常有用,允许生成各种可能的输入组合。其他用途包括解决数学问题和机会游戏
这叫做排列 这可以为您提供任何集合的排列:
public class Permutation {
public static IEnumerable<T[]> GetPermutations<T>(T[] items) {
int[] work = new int[items.Length];
for (int i = 0; i < work.Length; i++) {
work[i] = i;
}
foreach (int[] index in GetIntPermutations(work, 0, work.Length)) {
T[] result = new T[index.Length];
for (int i = 0; i < index.Length; i++) result[i] = items[index[i]];
yield return result;
}
}
public static IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len) {
if (len == 1) {
yield return index;
} else if (len == 2) {
yield return index;
Swap(index, offset, offset + 1);
yield return index;
Swap(index, offset, offset + 1);
} else {
foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1)) {
yield return result;
}
for (int i = 1; i < len; i++) {
Swap(index, offset, offset + i);
foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1)) {
yield return result;
}
Swap(index, offset, offset + i);
}
}
}
private static void Swap(int[] index, int offset1, int offset2) {
int temp = index[offset1];
index[offset1] = index[offset2];
index[offset2] = temp;
}
}
公共类置换{
公共静态IEnumerable GetPermutations(T[]项){
int[]工时=新int[items.Length];
for(int i=0;i
例如:
string[] items = { "one", "two", "three" };
foreach (string[] permutation in Permutation.GetPermutations<string>(items)) {
Console.WriteLine(String.Join(", ", permutation));
}
string[]items={“一”、“二”、“三”};
foreach(permutation.GetPermutations(items)中的字符串[]置换){
Console.WriteLine(String.Join(“,”,置换));
}
有关详细答案,请参阅:Donald Knuth,《计算机编程艺术》(又名TAOCP)。第4A卷,枚举和回溯,第7.2章。产生所有的可能性。
更新 下面是一组适用于不同场景的通用函数(需要.net 3.5或更高版本)。输出为{1,2,3,4}列表,长度为2 重复排列
static IEnumerable<IEnumerable<T>>
GetPermutationsWithRept<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutationsWithRept(list, length - 1)
.SelectMany(t => list,
(t1, t2) => t1.Concat(new T[] { t2 }));
}
static IEnumerable<IEnumerable<T>>
GetKCombsWithRept<T>(IEnumerable<T> list, int length) where T : IComparable
{
if (length == 1) return list.Select(t => new T[] { t });
return GetKCombsWithRept(list, length - 1)
.SelectMany(t => list.Where(o => o.CompareTo(t.Last()) >= 0),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
排列
static IEnumerable<IEnumerable<T>>
GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(o => !t.Contains(o)),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
static IEnumerable<IEnumerable<T>>
GetKCombs<T>(IEnumerable<T> list, int length) where T : IComparable
{
if (length == 1) return list.Select(t => new T[] { t });
return GetKCombs(list, length - 1)
.SelectMany(t => list.Where(o => o.CompareTo(t.Last()) > 0),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
K-重复组合
static IEnumerable<IEnumerable<T>>
GetPermutationsWithRept<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutationsWithRept(list, length - 1)
.SelectMany(t => list,
(t1, t2) => t1.Concat(new T[] { t2 }));
}
static IEnumerable<IEnumerable<T>>
GetKCombsWithRept<T>(IEnumerable<T> list, int length) where T : IComparable
{
if (length == 1) return list.Select(t => new T[] { t });
return GetKCombsWithRept(list, length - 1)
.SelectMany(t => list.Where(o => o.CompareTo(t.Last()) >= 0),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
K-组合
static IEnumerable<IEnumerable<T>>
GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(o => !t.Contains(o)),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
static IEnumerable<IEnumerable<T>>
GetKCombs<T>(IEnumerable<T> list, int length) where T : IComparable
{
if (length == 1) return list.Select(t => new T[] { t });
return GetKCombs(list, length - 1)
.SelectMany(t => list.Where(o => o.CompareTo(t.Last()) > 0),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
关于彭阳回答:
下面是我的通用函数,它可以返回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 }));
}
我运行了这个示例,但我不太确定结果是否正确
例2:n=3,k=3
IEnumerable<IEnumerable<int>> result =
GetCombinations(Enumerable.Range(1, 3), 3);
这不应该发生在组合中,否则它应该指定它是重复的。参见文章《古法给出的解决方案的另一个版本》。下面是该类的完整源代码:
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Permutation
{
public IEnumerable<T[]> GetPermutations<T>(T[] items)
{
var work = new int[items.Length];
for (var i = 0; i < work.Length; i++)
{
work[i] = i;
}
foreach (var index in GetIntPermutations(work, 0, work.Length))
{
var result = new T[index.Length];
for (var i = 0; i < index.Length; i++) result[i] = items[index[i]];
yield return result;
}
}
public IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len)
{
switch (len)
{
case 1:
yield return index;
break;
case 2:
yield return index;
Swap(index, offset, offset + 1);
yield return index;
Swap(index, offset, offset + 1);
break;
default:
foreach (var result in GetIntPermutations(index, offset + 1, len - 1))
{
yield return result;
}
for (var i = 1; i < len; i++)
{
Swap(index, offset, offset + i);
foreach (var result in GetIntPermutations(index, offset + 1, len - 1))
{
yield return result;
}
Swap(index, offset, offset + i);
}
break;
}
}
private static void Swap(IList<int> index, int offset1, int offset2)
{
var temp = index[offset1];
index[offset1] = index[offset2];
index[offset2] = temp;
}
}
}
使用System.Collections.Generic;
命名空间控制台应用程序1
{
公共类置换
{
公共IEnumerable GetPermutations(T[]项)
{
var work=新整数[items.Length];
对于(变量i=0;i{1, 1} {1, 2} {1, 3} {2, 1} {2, 2} {2, 3} {3, 1} {3, 2} {3, 3}
IEnumerable<IEnumerable<int>> result =
GetCombinations(Enumerable.Range(1, 3), 3);
{1, 1, 1} {1, 1, 2} {1, 1, 3}
{1, 2, 1} {1, 2, 2} {1, 2, 3}
{1, 3, 1} {1, 3, 2} {1, 3, 3}
{2, 1, 1} {2, 1, 2} {2, 1, 3}
{2, 2, 1} {2, 2, 2} {2, 2, 3}
{2, 3, 1} {2, 3, 2} {2, 3, 3}
{3, 1, 1} {3, 1, 2} {3, 1, 3}
{3, 2, 1} {3, 2, 2} {3, 2, 3}
{3, 3, 1} {3, 3, 2} {3, 3, 3}
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Permutation
{
public IEnumerable<T[]> GetPermutations<T>(T[] items)
{
var work = new int[items.Length];
for (var i = 0; i < work.Length; i++)
{
work[i] = i;
}
foreach (var index in GetIntPermutations(work, 0, work.Length))
{
var result = new T[index.Length];
for (var i = 0; i < index.Length; i++) result[i] = items[index[i]];
yield return result;
}
}
public IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len)
{
switch (len)
{
case 1:
yield return index;
break;
case 2:
yield return index;
Swap(index, offset, offset + 1);
yield return index;
Swap(index, offset, offset + 1);
break;
default:
foreach (var result in GetIntPermutations(index, offset + 1, len - 1))
{
yield return result;
}
for (var i = 1; i < len; i++)
{
Swap(index, offset, offset + i);
foreach (var result in GetIntPermutations(index, offset + 1, len - 1))
{
yield return result;
}
Swap(index, offset, offset + i);
}
break;
}
}
private static void Swap(IList<int> index, int offset1, int offset2)
{
var temp = index[offset1];
index[offset1] = index[offset2];
index[offset2] = temp;
}
}
}
private static IEnumerable<string> FindPermutations(string set)
{
var output = new List<string>();
switch (set.Length)
{
case 1:
output.Add(set);
break;
default:
output.AddRange(from c in set let tail = set.Remove(set.IndexOf(c), 1) from tailPerms in FindPermutations(tail) select c + tailPerms);
break;
}
return output;
}
Console.WriteLine("Enter a sting ");
var input = Console.ReadLine();
foreach (var stringCombination in FindPermutations(input))
{
Console.WriteLine(stringCombination);
}
Console.ReadLine();
// 1. remove first char
// 2. find permutations of the rest of chars
// 3. Attach the first char to each of those permutations.
// 3.1 for each permutation, move firstChar in all indexes to produce even more permutations.
// 4. Return list of possible permutations.
public static string[] FindPermutationsSet(string word)
{
if (word.Length == 2)
{
var c = word.ToCharArray();
var s = new string(new char[] { c[1], c[0] });
return new string[]
{
word,
s
};
}
var result = new List<string>();
var subsetPermutations = (string[])FindPermutationsSet(word.Substring(1));
var firstChar = word[0];
foreach (var temp in subsetPermutations.Select(s => firstChar.ToString() + s).Where(temp => temp != null).Where(temp => temp != null))
{
result.Add(temp);
var chars = temp.ToCharArray();
for (var i = 0; i < temp.Length - 1; i++)
{
var t = chars[i];
chars[i] = chars[i + 1];
chars[i + 1] = t;
var s2 = new string(chars);
result.Add(s2);
}
}
return result.ToArray();
}
Console.WriteLine("Enter a sting ");
var input = Console.ReadLine();
Console.WriteLine("Here is all the possable combination ");
foreach (var stringCombination in FindPermutationsSet(input))
{
Console.WriteLine(stringCombination);
}
Console.ReadLine();
private static void CombinationsOfItemsInAnArray()
{
int[] arr = { 10, 50, 3, 1, 2 }; //unique elements
var numberSet = new HashSet<int>();
var combinationList = new List<Tuple<int, int>>();
foreach (var number in arr)
{
if (!numberSet.Contains(number))
{
//create all tuple combinations for the current number against all the existing number in the number set
foreach (var item in numberSet)
combinationList.Add(new Tuple<int, int>(number, item));
numberSet.Add(number);
}
}
foreach (var item in combinationList)
{
Console.WriteLine("{{{0}}} - {{{1}}}",item.Item1,item.Item2);
}
}
{50} - {10}
{3} - {10}
{3} - {50}
{1} - {10}
{1} - {50}
{1} - {3}
{2} - {10}
{2} - {50}
{2} - {3}
{2} - {1}
internal HashSet<string> GetAllPermutations(IEnumerable<int> numbers)
{
HashSet<string> results = new HashSet<string>();
if (numbers.Count() > 0)
results.Add(string.Join(",", new SortedSet<int>(numbers)));
for (int i = 0; i <= numbers.Count() - 1; i++)
{
List<int> newNumbers = new List<int>(numbers);
newNumbers.RemoveAt(i);
results.UnionWith(GetAllPermutations(newNumbers));
}
return results;
}