caozhy使用枚举器求索引的全排列是个好办法,学习了一下,重新写了一个函数。/// <summary>
/// 使用枚举器生成[n, count)索引全排列 。
/// </summary>
/// <param name="count">数目,索引在[0, count - 1]间 。</param>
/// <returns>索引全排列 。</returns>
/// <exception cref="ArgumentOutOfRangeException">如果count非法,抛出ArgumentOutOfRangeException 。</exception>
public static IEnumerable<IList<int>> PermutationIndice(int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException("count");
} IEnumerable<int> allIndice = Enumerable.Range(0, count);
IEnumerable<IList<int>> permutation = null;
for (int i = 0; i < count; i++)
{ // 每次迭代,求i个索引的排列全集
if (i == 0)
{
permutation = Enumerable.Select<int, IList<int>>(allIndice, x => new int[] { x });
//permutation = allIndice.Select<IList<int>>(x => new int[] { x });
}
else
{
permutation = Enumerable.SelectMany<IList<int>, IList<int>>(permutation,
l => Enumerable.Select<int, IList<int>>(allIndice.Except(l), x => new List<int>(l).Concat(new int[] { x }).ToList()));
//l => allIndice.Except(l).SelectMany<IList<int>>(x => new List<int>(new List<int>(l).Concat(new int[] { x}).ToList())));
}
} return permutation;
}
/// 使用枚举器生成[n, count)索引全排列 。
/// </summary>
/// <param name="count">数目,索引在[0, count - 1]间 。</param>
/// <returns>索引全排列 。</returns>
/// <exception cref="ArgumentOutOfRangeException">如果count非法,抛出ArgumentOutOfRangeException 。</exception>
public static IEnumerable<IList<int>> PermutationIndice(int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException("count");
} IEnumerable<int> allIndice = Enumerable.Range(0, count);
IEnumerable<IList<int>> permutation = null;
for (int i = 0; i < count; i++)
{ // 每次迭代,求i个索引的排列全集
if (i == 0)
{
permutation = Enumerable.Select<int, IList<int>>(allIndice, x => new int[] { x });
//permutation = allIndice.Select<IList<int>>(x => new int[] { x });
}
else
{
permutation = Enumerable.SelectMany<IList<int>, IList<int>>(permutation,
l => Enumerable.Select<int, IList<int>>(allIndice.Except(l), x => new List<int>(l).Concat(new int[] { x }).ToList()));
//l => allIndice.Except(l).SelectMany<IList<int>>(x => new List<int>(new List<int>(l).Concat(new int[] { x}).ToList())));
}
} return permutation;
}
解决方案 »
- 如何定义不能创建的类?
- C#写几行代码,发个声音能做到吗?
- DataGridViewCheckBoxColumn绑定int列的问题
- 顶者有分,关于Excel导出问题, 怎么样设置Excel导出时候的,列宽, 如果太长再换行
- C#怎么把PNG图片转换成8位深度
- 基于B/S架构的报价系统
- 如何使LISTVIEW 或LISTBOX 中的子项可以编辑。用户点击某单元格后可修改其值??
- 送全分,未在本地计算机上注册“Microsoft.Jet.OLEDB4.0”提供程序。
- C#windows窗体程序调用webService问题
- 求c#远程控制
- 莫名奇妙被报告说是病毒
- 请问SqlParameter.SourceColumn属性,这里怎么解释?
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var result = Arrange(4);
foreach (var item in result)
{
Console.WriteLine(string.Join(",", item.Select(x => x.ToString()).ToArray()));
}
} static IEnumerable<IEnumerable<int>> Arrange(int n)
{
IEnumerable<IEnumerable<int>> result = Enumerable.Range(0, n).Select(x => new List<int>() { x });
while (result.First().Count() < n)
{
result = result.SelectMany(x => Enumerable.Range(0, n).Except(x).Select(y => x.Concat(new List<int>() { y })));
}
return result;
}
}
}
这样?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var result = Arrange(4);
foreach (var item in result)
{
Console.WriteLine(string.Join(",", item.Select(x => x.ToString()).ToArray()));
}
} static IEnumerable<IEnumerable<int>> Arrange(int n, IEnumerable<IEnumerable<int>> seed = null)
{
if (seed == null)
seed = Enumerable.Range(0, n).Select(x => new List<int>() { x });
if (seed.First().Count() == n) return seed;
return Arrange(n, seed.SelectMany(x => Enumerable.Range(0, n).Except(x).Select(y => x.Concat(new List<int>() { y }))));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var result = Arrange(4);
foreach (var item in result)
{
Console.WriteLine(string.Join(",", item.Select(x => x.ToString()).ToArray()));
}
} static IEnumerable<IEnumerable<int>> Arrange(int n, IEnumerable<IEnumerable<int>> seed = null)
{
if (seed == null)
seed = Enumerable.Range(0, n).Select(x => new List<int>() { x });
if (seed.First().Count() == n)
{
foreach (var item in seed)
yield return item;
}
else
{
foreach (var item in Arrange(n, seed.SelectMany(x => Enumerable.Range(0, n).Except(x).Select(y => x.Concat(new List<int>() { y })))))
yield return item;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] charString = { "L", "O", "V", "E" };
for (int i = 0; i < Total.Count; i++)
{
Console.WriteLine(Total[i]);
}
Console.ReadKey();
}
List<string> Total = new List<string>();
/// <summary>
/// 全排列
/// </summary>
/// <param name="charString">排列源字符</param>
/// <param name="count">排列数目</param>
/// <param name="str"></param>
public void test(string[] charString, int count, string str="")
{
foreach (string wd in charString)
{
string newStr = str + wd;
if (newStr.Length > count-1)
{
Total.Add(newStr);
}
else
{
test(charString, count, newStr);
}
}
}
}
}.
.
抄袭加改造。这个算不算
.
.