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
复制代码 代码如下:
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
栏 目:.NET代码
下一篇:.Net Web Api中利用FluentValidate进行参数验证的方法
本文标题:C#组合函数的使用详解
本文地址:http://www.codeinn.net/misctech/15768.html