C# 我怎样才能得到输出字符串数组的所有可能的组合?
我需要任何字符串列表输出所有的可能性。像那样。 这就是720个不同的字符串C# 我怎样才能得到输出字符串数组的所有可能的组合?,c#,combinations,C#,Combinations,我需要任何字符串列表输出所有的可能性。像那样。 这就是720个不同的字符串 List<String> items = new List<String>(); items.AddRange(new String[] { "a", "b", "c", "d", "e", "f" }); List items=newlist(); AddRa
List<String> items = new List<String>();
items.AddRange(new String[] { "a", "b", "c", "d", "e", "f" });
List items=newlist();
AddRange(新字符串[]{“a”、“b”、“c”、“d”、“e”、“f”});
产出
1-)a b c d e f
2-)a b c d f e
3-)a b c e d f
4-)a b c e f d
5-)a b c f d e
6-)a b c f e d
7-)a b d c e f
8-)a b d c f e
9-)a b d e c f
10-)a b d e f c
11-
12-
13-)b a c d e f
720-
给你:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
List<String> items = new List<String>();
items.AddRange(new String[] { "a", "b", "c", "d", "e", "f" });
int i = 0;
foreach (var permutation in Permute(items))
Console.WriteLine(++i + ": " + string.Join(" ", permutation));
}
public static IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> seq)
{
return
from item in seq.Select((value, index) => new {value, index})
from remainder in seq.Count() == 1 ? new[]{new T[0]} : Permute(allExcept(seq, item.index))
select new[]{item.value}.Concat(remainder);
}
static IEnumerable<T> allExcept<T>(IEnumerable<T> seq, int indexToSkip)
{
return
from item in seq.Select((value, index) => new {value, index})
where item.index != indexToSkip
select item.value;
}
}
}
其中DistinctBy()
是IEnumerable
的扩展方法:
公共静态类EnumerableText
{
公共静态IEnumerable DistinctBy(此IEnumerable源,Func键选择器)
{
返回source.DistinctBy(keySelector,null);
}
公共静态IEnumerable DistinctBy(
这是一个数不清的来源,
Func键选择器,
IEqualityComparer(比较器)
{
返回distinctByImpl(源、键选择器、比较器);
}
静态IEnumerable distinctByImpl(
IEnumerable来源,
Func键选择器,
IEqualityComparer(比较器)
{
var knownKeys=新哈希集(比较器);
返回source.Where(element=>knownKeys.Add(keySelector(element));
}
}
给你:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
List<String> items = new List<String>();
items.AddRange(new String[] { "a", "b", "c", "d", "e", "f" });
int i = 0;
foreach (var permutation in Permute(items))
Console.WriteLine(++i + ": " + string.Join(" ", permutation));
}
public static IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> seq)
{
return
from item in seq.Select((value, index) => new {value, index})
from remainder in seq.Count() == 1 ? new[]{new T[0]} : Permute(allExcept(seq, item.index))
select new[]{item.value}.Concat(remainder);
}
static IEnumerable<T> allExcept<T>(IEnumerable<T> seq, int indexToSkip)
{
return
from item in seq.Select((value, index) => new {value, index})
where item.index != indexToSkip
select item.value;
}
}
}
其中DistinctBy()
是IEnumerable
的扩展方法:
公共静态类EnumerableText
{
公共静态IEnumerable DistinctBy(此IEnumerable源,Func键选择器)
{
返回source.DistinctBy(keySelector,null);
}
公共静态IEnumerable DistinctBy(
这是一个数不清的来源,
Func键选择器,
IEqualityComparer(比较器)
{
返回distinctByImpl(源、键选择器、比较器);
}
静态IEnumerable distinctByImpl(
IEnumerable来源,
Func键选择器,
IEqualityComparer(比较器)
{
var knownKeys=新哈希集(比较器);
返回source.Where(element=>knownKeys.Add(keySelector(element));
}
}
那么你尝试了什么?b谢谢你,我现在正在尝试。你怎么知道它给了你不同的字符串?@S.Akbari因为6!=第一个字符有6个可用位置。第二个字符有5个可用位置,第三个字符有4个位置,依此类推…:6*5*4*3*2*1=720。你试过什么了?B谢谢你,我现在正在试。你怎么知道它会给你不同的字符串?@S.Akbari因为6!=第一个字符有6个可用位置。第二个字符有5个可用位置,第三个字符有4个位置,依此类推…:6*5*4*3*2*1=720.Thanx成功了,是你写的吗?@MuratcanOguzhan我写了排列代码,但可枚举扩展方法最初是Jon Skeet写的,我想,作为他的Morelink项目的一部分:谢谢你@MatthewWatson,我非常感谢。@MuratcanOguzhan供你参考:我用上次编辑简化了代码。是的,最后一次编辑看起来更简单。再次感谢你。我改变了方法。Thanx成功了,是你写的吗?@MuratcanOguzhan我写了排列代码,但可枚举扩展方法最初是Jon Skeet写的,我想,作为他的Morelink项目的一部分:谢谢你@MatthewWatson,我非常感谢。@MuratcanOguzhan供参考:我用上次编辑简化了代码。是的,上次编辑看起来更简单。再次感谢你。我改变了方法。
public static class EnumerableExt
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return source.DistinctBy(keySelector, null);
}
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer)
{
return distinctByImpl(source, keySelector, comparer);
}
static IEnumerable<TSource> distinctByImpl<TSource, TKey>(
IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer)
{
var knownKeys = new HashSet<TKey>(comparer);
return source.Where(element => knownKeys.Add(keySelector(element)));
}
}