排列在c# [英] permutation in c#

查看:108
本文介绍了排列在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能产生在C#中收集的所有排列?



 的char [] inputSet = {'A​​', B,C}; 
置换<&烧焦GT;排列=新的置换和LT;焦炭>(inputSet);
的foreach(IList的<焦炭指p在排列)
{
Console.WriteLine(的String.Format({{{0} {1} {2}}},P [0 ],第[1],第[2]));
}


解决方案

我已经面对的问题,我写了这些简单的方法:

 公共静态的IList< T []> GeneratePermutations< T>(T [] OBJ文件,长限制吗?)
{
VAR的结果=新的List< T []>();
长N =阶乘(objs.Length);
N =(limit.HasValue || limit.Value> N!)? N:(limit.Value);

为(长K = 0; K< N; k ++)
{
T [] kperm = GenerateKthPermutation< T>(K,OBJ文件);
result.Add(kperm);
}

返回结果;
}

公共静态T [] GenerateKthPermutation< T>(长K,T [] OBJ文件)
{
T [] permutedObjs =新的T [OBJ文件。长度];

的for(int i = 0; I< objs.Length;我++)
{
permutedObjs [I] = OBJ文件[I]
}
为(INT J = 2; J< objs.Length + 1; J ++)
{
K = K /(J - 1); //整数除法切断其余
长I1 =(K%j)条;
长I2 =的J - 1;
如果
{$ B $(B T)= tmpObj1 permutedObjs [I1](I1 = I2!); $ B $(B T)= tmpObj2 permutedObjs [12];
permutedObjs [I1] = tmpObj2;
permutedObjs [12] = tmpObj1;
}
}
返回permutedObjs;
}

公共静态长因子(INT N)
{
如果(N小于0){抛出新的异常(为阶乘未接受输入); } //错误结果 - 未定义
如果(N> 256){抛出新的异常(输入太大了,阶乘); } //错误结果 - 输入太大

如果(N == 0){返回1; }

//计算阶乘的迭代而不是递归:

长tempResult = 1;
的for(int i = 1; I< = N;我++)
{
tempResult * = I;
}
返回tempResult;
}



用法:

  VAR烫发= Utilities.GeneratePermutations<焦炭>(新的char [] {'A','B','C'},NULL); 


Is it possible to generate all permutations of a collection in c#?

char[] inputSet = { 'A','B','C' };
Permutations<char> permutations = new Permutations<char>(inputSet);
foreach (IList<char> p in permutations)
{
   Console.WriteLine(String.Format("{{{0} {1} {2}}}", p[0], p[1], p[2]));
}

解决方案

I've already faced the problem and I wrote these simple methods:

    public static IList<T[]> GeneratePermutations<T>(T[] objs, long? limit)
    {
        var result = new List<T[]>();
        long n = Factorial(objs.Length);
        n = (!limit.HasValue || limit.Value > n) ? n : (limit.Value);

        for (long k = 0; k < n; k++)
        {
            T[] kperm = GenerateKthPermutation<T>(k, objs);
            result.Add(kperm);
        }

        return result;
    }

    public static T[] GenerateKthPermutation<T>(long k, T[] objs)
    {
        T[] permutedObjs = new T[objs.Length];

        for (int i = 0; i < objs.Length; i++)
        {
            permutedObjs[i] = objs[i];
        }
        for (int j = 2; j < objs.Length + 1; j++)
        {
            k = k / (j - 1);                      // integer division cuts off the remainder
            long i1 = (k % j);
            long i2 = j - 1;
            if (i1 != i2)
            {
                T tmpObj1 = permutedObjs[i1];
                T tmpObj2 = permutedObjs[i2];
                permutedObjs[i1] = tmpObj2;
                permutedObjs[i2] = tmpObj1;
            }
        }
        return permutedObjs;
    }

    public static long Factorial(int n)
    {
        if (n < 0) { throw new Exception("Unaccepted input for factorial"); }    //error result - undefined
        if (n > 256) { throw new Exception("Input too big for factorial"); }  //error result - input is too big

        if (n == 0) { return 1; }

        // Calculate the factorial iteratively rather than recursively:

        long tempResult = 1;
        for (int i = 1; i <= n; i++)
        {
            tempResult *= i;
        }
        return tempResult;
    }

Usage:

var perms = Utilities.GeneratePermutations<char>(new char[]{'A','B','C'}, null);

这篇关于排列在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆