C#组合函数

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值