C#組合函數(shù)的使用詳解
更新時間:2013年06月09日 09:49:41 作者:
本篇文章是對C#中的組合函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
如下所示:
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>();
//將數(shù)組元素添加到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中的鍵與數(shù)組的中對應(yīng)索引比dd中的value大進(jìn)行組合
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);
}
}
}
結(jié)果
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
復(fù)制代碼 代碼如下:
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>();
//將數(shù)組元素添加到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中的鍵與數(shù)組的中對應(yīng)索引比dd中的value大進(jìn)行組合
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);
}
}
}
結(jié)果
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
相關(guān)文章
C#實(shí)現(xiàn)xml文件反序列化讀入數(shù)據(jù)到object的方法
這篇文章主要介紹了C#實(shí)現(xiàn)xml文件反序列化讀入數(shù)據(jù)到object的方法,涉及C#操作XML文件類型轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法示例
這篇文章主要介紹了C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法,結(jié)合具體實(shí)例形式分析了數(shù)據(jù)庫的創(chuàng)建及checkedListBox1控件連接數(shù)據(jù)庫的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
C#中datagridview的EditingControlShowing事件用法實(shí)例
這篇文章主要介紹了C#中datagridview的EditingControlShowing事件用法,實(shí)例分析了datagridview的EditingControlShowing事件的定義與使用技巧,需要的朋友可以參考下2015-06-06
C#實(shí)現(xiàn)餐飲管理系統(tǒng)完整版
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)餐飲管理系統(tǒng)的完整版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
WPF利用ScottPlot實(shí)現(xiàn)動態(tài)繪制圖像
ScottPlot是基于.Net的一款開源免費(fèi)的交互式可視化庫,支持Winform和WPF等UI框架,本文主要為大家詳細(xì)介紹了如何WPF如何使用ScottPlot實(shí)現(xiàn)動態(tài)繪制圖像,需要的可以參考下2023-12-12

