C# 排列组合取值
2015-06-16 10:05
447 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; class Program { static void Main(string[] args) { //List<string> list = new List<string>(); //for (int i = 0; i < 6; i++) //{ // list.Add(i.ToString()); //} List<string> list = new List<string> { "0", "1", "2", "5", "8", "4", "12", "32" }; Combination.C(list, 3); Console.WriteLine("---------------------"); //Console.Read(); Combination.A(list, 0, 3); Console.WriteLine("---------------------"); Console.Read(); } } public abstract class Combination { public static List<string> GetCombination(List<string> SampleList, int m) { if (m == 1) { return SampleList; } List<string> result = new List<string>(); if (SampleList.Count == m) { StringBuilder temp_sb = new StringBuilder(); foreach (string s in SampleList) { temp_sb.Append(s); } result.Add(temp_sb.ToString()); Console.WriteLine(temp_sb.ToString()); return result; } string temp_firstelement = SampleList[0]; SampleList.RemoveAt(0); List<string> temp_samplist1 = new List<string>(); temp_samplist1.AddRange(SampleList); List<string> temp_list1 = GetCombination(temp_samplist1, m - 1); foreach (string s in temp_list1) { result.Add(temp_firstelement + s); Console.WriteLine(temp_firstelement + s); } List<string> temp_samplist2 = new List<string>(); temp_samplist2.AddRange(SampleList); List<string> temp_list2 = GetCombination(temp_samplist2, m); result.AddRange(temp_list2); return result; } /// <summary> /// 对数组进行组合操作,选取selectCount个元素进行组合 /// </summary> /// <param name="lsArray">即将进行组合操作的数组</param> /// <param name="selectCount">选取的元素的个数</param> public static void C(List<string> lsArray, int selectCount) { int totolcount = lsArray.Count; int[] currectselect = new int[selectCount]; int last = selectCount - 1; for (int i = 0; i < selectCount; i++) { currectselect[i] = i; } while (true) { for (int i = 0; i < selectCount; i++) { Console.Write(" {0} ", lsArray[currectselect[i]]); } Console.WriteLine(); if (currectselect[last] < totolcount - 1) { currectselect[last]++; } else { int pos = last; while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1) { pos--; } if (pos == 0) return; currectselect[pos - 1]++; for (int i = pos; i < selectCount; i++) { currectselect[i] = currectselect[i - 1] + 1; } } } } /// <summary> /// 对数组进行全排列 /// </summary> /// <param name="lsArray">要进行全排列的数组</param> /// <param name="begin">进行全排列的开始下标</param> /// <param name="end">进行全排列的结束下标</param> public static void A(List<string> lsArray, int begin, int end) { if (begin == end) { for (int i = 0; i <= end; i++) Console.Write(" {0} ", lsArray[i]); Console.WriteLine(); } for (int i = begin; i <= end; i++) { Swap(lsArray, begin, i); A(lsArray, begin + 1, end); Swap(lsArray, begin, i); } } /// <summary> /// 交换数组中的下标为x,y的值 /// </summary> /// <param name="lsArray">该数组</param> /// <param name="x"></param> /// <param name="y"></param> static void Swap(List<string> lsArray, int x, int y) { string t = lsArray[x]; lsArray[x] = lsArray[y]; lsArray[y] = t; } }
相关文章推荐
- C#图片上传效果实例分析
- C#高级编程十六天----匿名类型
- C#获取设置IP地址,启用禁用适配器
- .Net xsd工具生成C#代码的一些方式
- 简单谈谈C#中深拷贝、浅拷贝
- 简单记录C# 条件编译
- 一个简单的C#命令行参数程序
- C#数据绑定
- C# 条件编译备忘
- C# Linq: Join两个DataTable
- c#DataReader和DataSet的异同
- C# 条件编译备忘
- C#索引器
- 用C#和Thrift来对Hbase进行访问
- DEV GridControl小结。。 作者:爱搁浅
- C#高级编程十五天----只读字段
- c# 扩展方法奇思妙用基础:IsBetween 通用扩展
- c# 扩展方法奇思妙用基础:string 常用扩展
- c# 扩展方法奇思妙用基础:WhereIf 扩展
- 关于C#控制台输出中文字符乱码解决方案