使用枚举器求索引的全排列

hmlb1981 2012-09-13 10:10:36
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;
}
...全文
292 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
寂小魔 2013-03-21
  • 打赏
  • 举报
回复

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);
                }
            }
        }
    }
}
. . 抄袭加改造。这个算不算 . .
threenewbee 2012-09-13
  • 打赏
  • 举报
回复
用yield return展开:
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;
}
}
}
}
threenewbee 2012-09-13
  • 打赏
  • 举报
回复
顺便贴一个递归版本的:
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 }))));
}
}
}
hmlb1981 2012-09-13
  • 打赏
  • 举报
回复
是的,但是程序里没有注释,因此花了一些时间理解思路。
threenewbee 2012-09-13
  • 打赏
  • 举报
回复
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>> 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;
}
}
}

这样?

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧