c#组合算法整理
2009-02-24 11:38
232 查看
原帖http://topic.csdn.net/u/20090220/23/e2d130d9-d7d4-4520-bec7-e78ae6ca9aff.html
感谢litaoye和min_jie
感谢litaoye和min_jie
static List<string> GetCombinationF3(string[] data, int count) { Dictionary<string, int> dic = new Dictionary<string, int>(); List<string> output = new List<string>(); for (int i = 0; i < data.Length; i++) { dic.Add(data[i], i); } SelectN(dic, data, count, 1, ref output); return output; } static void SelectN(Dictionary<string, int> dd, string[] data, int count, int times, ref List<string> output) { Dictionary<string, int> dic = new Dictionary<string, int>(); foreach (KeyValuePair<string, int> kv in dd) { for (int i = kv.Value + 1; i < data.Length; i++) { if (times < count - 1) { dic.Add(kv.Key + "/t" + data[i], i); } else { output.Add(kv.Key + "/t" + data[i]);//不考虑输出,将此句注释掉 } } } times++; if (dic.Count > 0) SelectN(dic, data, count, times,ref output); } static List<string> GetCombinationF1(string[] set,int m) { int n = set.Length; int min = (0x01 << m) - 1;//00111111 int max = min << (n - m);//11111100 int j; int k; List<string> output = new List<string>(); string s; for (int i = min; i <= max; i++) { j = 0; k = i; while (k > 0) { j += (int)(k & 0x01); k >>= 1; if (j > m) { break; } } if (j == m) { s = ""; k = 0x01; for (int l = n - 1; l >= 0; l--) { if ((k & i) == k) { s+=set[l] + "/t"; } k <<= 1; } output.Add(s); } } return output; } static List<string> GetCombinationF2(string[] strArray, int selectCount) { int totalCount = strArray.Length; int[] currentSelect = new int[selectCount]; int last = selectCount - 1; List<string> output = new List<string>(); string s; //付初始值 for (int i = 0; i < selectCount; i++) currentSelect[i] = i; while (true) { s = ""; //输出部分,生成的时候从0计数,所以输出的时候+1 for (int i = 0; i < selectCount; i++) { s += strArray[currentSelect[i]]+"/t"; } output.Add(s); //如果不进位 if (currentSelect[last] < totalCount - 1) currentSelect[last]++; else { //进位部分 int position = last; while (position > 0 && currentSelect[position - 1] == currentSelect[position] - 1) position--; if (position == 0) break ; currentSelect[position - 1]++; for (int i = position; i < selectCount; i++) currentSelect[i] = currentSelect[i - 1] + 1; } } return output; } static List<string> GetCombinationF4(string[] data, int count) { List<string> output = new List<string>(); int len = data.Length; string start = "1".PadRight(count, '1').PadRight(len, '0'); string s; while (start != string.Empty) { s = ""; for (int i = 0; i < len; i++) if (start[i] == '1') s+=data[i] + "/t"; output.Add(s); start = GetNext(start); } return output; } static string GetNext(string str) { string next = string.Empty; int pos = str.IndexOf("10"); if (pos < 0) return next; else if (pos == 0) return "01" + str.Substring(2); else { int len = str.Length; next = str.Substring(0, pos).Replace("0", "").PadRight(pos, '0') + "01"; if (pos < len - 2) next += str.Substring(pos + 2); } return next; }
相关文章推荐
- C# 组合算法
- 关于算法—— 一维字符串数组之间组合问题的C#实现
- C#实现排列组合算法
- C#组合算法 (2)
- C# 组合算法
- 基于C#的排列和组合算法
- C#实现排列组合算法
- 排列 组合 算法 C#
- C#实现排列组合算法
- C# 计算排列组合数,及列出所有组合形式的算法
- C#语法灵活运用之排列组合算法
- 面试的算法1(C语言)(整理)(组合数 字符串倒置 最大公共串)
- c#组合算法
- C#实现排列组合算法完整实例
- C#中实现任意List的全组合算法代码
- 全排列和组合算法的C#语言实现
- Asp.Net/C#/WCF - C#实现谷歌相似图片查询算法
- 国际象棋“皇后”问题的回溯算法[C#源码分析]
- 腾讯QQ web新版登陆加密算法 js算法 c#算法 java算法
- GC算法精解(复制算法与标记/整理算法)