zoukankan      html  css  js  c++  java
  • C#实现的排列与组合(Permutation & Combination)算法

    I wrote two generic class to implement these two algorithms, so you can use these classes to generate permutations and combinations for some use, such as software testing.

    Using the Code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace CombinationAlgorithmic
    {
        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {
                ShowCombinations
    <int>(new int[] 12345 }3);
                ShowPermutations
    <string>(new string[] "dog""cat""bird""bat" }4);
            }


            
    static void ShowPermutations<T>(T[] array, int length)
            
    {
                Permutations
    <T> permutations = new Permutations<T>(array, length);
                Console.WriteLine(
    "{0} Permutations of Array \"{1}\":\n", permutations.Count, GetString<T>(array));
                
    foreach (IList<T> perm in permutations)
                
    {
                    Console.WriteLine(GetString
    <T>(perm) + "\n");
                }

            }


            
    static void ShowCombinations<T>(T[] array, int length)
            
    {
                Combinations
    <T> combinations = new Combinations<T>(array, length);
                Console.WriteLine(
    "{0} Combinations of Array \"{1}\":\n", combinations.Count, GetString<T>(array));
                
    foreach (IList<T> oneCom in combinations)
                
    {
                    Console.WriteLine(GetString
    <T>(oneCom) + "\n");
                }

            }


            
    static string GetString<T>(IList<T> list)
            
    {
                StringBuilder sb 
    = new StringBuilder();
                
    foreach (T item in list)
                
    {
                    sb.Append(item.ToString() 
    + ",");
                }

                sb.Remove(sb.Length
    -11);
                
    return sb.ToString();
            }

        }

    }



    output:
    10 Combinations of Array "1,2,3,4,5":

    1,2,3

    1,2,4

    1,2,5

    1,3,4

    1,3,5

    1,4,5

    2,3,4

    2,3,5

    2,4,5

    3,4,5

    24 Permutations of Array "dog,cat,bird,bat":

    dog,cat,bird,bat

    dog,cat,bat,bird

    dog,bird,cat,bat

    dog,bat,cat,bird

    dog,bird,bat,cat

    dog,bat,bird,cat

    cat,dog,bird,bat

    cat,dog,bat,bird

    bird,dog,cat,bat

    bat,dog,cat,bird

    bird,dog,bat,cat

    bat,dog,bird,cat

    cat,bird,dog,bat

    cat,bat,dog,bird

    bird,cat,dog,bat

    bat,cat,dog,bird

    bird,bat,dog,cat

    bat,bird,dog,cat

    cat,bird,bat,dog

    cat,bat,bird,dog

    bird,cat,bat,dog

    bat,cat,bird,dog

    bird,bat,cat,dog

    bat,bird,cat,dog

    Download the Source Project:
    https://files.cnblogs.com/Dah/PermutationCombination.rar

    Note: If you find any bug or have some suggestion, please send E-Mail to me or leave your comments here, thanks!
  • 相关阅读:
    springcloud组件梳理之hystrix
    springcloud组件梳理之Feign
    React Native startReactApplication 方法简析
    FREE OFFER
    修改docker默认目录
    使用云效进行自动化构建和部署
    Git同时推送到多个远端仓库【转】
    OneNote出现we're sorry. OneNote is cleanning up from the last time it was open.
    【数学基础】数据科学的概率基础
    【摄影后期基础教程】Lightroom_秋凉视频教程
  • 原文地址:https://www.cnblogs.com/Dah/p/572183.html
Copyright © 2011-2022 走看看