当前位置:主页 > 软件编程 > .NET代码 >

C#组合函数的使用详解

时间:2020-10-25 14:29:38 | 栏目:.NET代码 | 点击:

如下所示:
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{

    class Class1
    {
        static string[] str = { "A", "B", "C", "D", "E" };
        static void Main()
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            //将数组元素添加到dic
            for (int i = 0; i < str.Length; i++)
            {
                dic.Add(str[i], i);
            }
            GetDic(dic);
            Console.ReadLine();
        }
        static void GetDic(Dictionary<string, int> dd)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            //主要是抓住AB,AC,AD,AE,然后就是开始从 BC,BD这样的原理用dd中的键与数组的中对应索引比dd中的value大进行组合
            foreach (KeyValuePair<string, int> kk in dd)
            {
                for (int i = kk.Value + 1; i < str.Length; i++)
                {
                    Console.WriteLine(kk.Key + str[i]);
                    dic.Add(kk.Key + str[i], i);
                }
            }
            //递归
            if (dic.Count > 0) GetDic(dic);
        }
    }

}
结果
AB
AC
AD
AE
BC
BD
BE
CD
CE
DE
ABC
ABD
ABE
ACD
ACE
ADE
BCD
BCE
BDE
CDE
ABCD
ABCE
ABDE
ACDE
BCDE
ABCDE

您可能感兴趣的文章:

相关文章