View Code
static void Main(string[] args) { List<int> dataList = new List<int>(); //dataList.Add(3); //dataList.Add(3); //dataList.Add(2); //dataList.Add(5); //dataList.Add(1); //dataList.Add(4); //dataList.Add(2); List<List<int>> selectIndex = GetListIndex(dataList); foreach (var oneList in selectIndex) { foreach (var item in oneList) { Console.Write(item); Console.Write(" "); } Console.WriteLine(); } Console.ReadKey(); } static List<List<int>> GetListIndex(List<int> dataList) { List<List<int>> rtn = new List<List<int>>(); List<int> initNum = new List<int>(); foreach (var item in dataList) { initNum.Add(0); } //获得所有 数字的集合 bool isLast = false; while (true) { rtn.Add(initNum); initNum = GetNextNum(initNum, dataList, ref isLast); if (isLast == true) break; } return rtn; } static List<int> GetNextNum(List<int> num, List<int> digitRadix, ref bool isLast) { List<int> rtn = new List<int>(); bool isNextAdd = false; for (int i = 0; i < num.Count(); i++) { int thisDigit = num[i]; if ((i == 0) || (isNextAdd == true)) { thisDigit++; if (thisDigit >= digitRadix[i]) { isNextAdd = true; thisDigit = 0; } else { isNextAdd = false; } } if ((i == num.Count() - 1) && (isNextAdd == true)) { isLast = true; } rtn.Add(thisDigit); } return rtn; }
static void Main(string[] args) { int count = 3; //Stopwatch sw = new Stopwatch(); //sw.Start(); //List<List<int>> selectIndex = GetOneListIndex(count); //sw.Stop(); //Console.WriteLine("总运行时间:" + sw.Elapsed); Stopwatch sw2 = new Stopwatch(); sw2.Start(); List<List<int>> selectIndex2 = GetOneListIndex2(count); sw2.Stop(); Console.WriteLine("总运行时间:" + sw2.Elapsed); foreach (var oneList in selectIndex2) { foreach (var item in oneList) { Console.Write(item); Console.Write(" "); } Console.WriteLine(); } Console.ReadKey(); } static List<List<int>> GetOneListIndex(int digit) { List<List<int>> rtn = new List<List<int>>(); //获得位数的字符串“222” StringBuilder digitRadix = new StringBuilder(); StringBuilder initNum = new StringBuilder(); for (int i = 0; i < digit; i++) { digitRadix.Append("2"); initNum.Append("0"); } //获得所有 数字的集合 List<string> allCollection = new List<string>(); string num = initNum.ToString(); while (true) { num = GetNextNum(num, digitRadix.ToString()); if (num == initNum.ToString()) break; allCollection.Add(num); } //获得所有的组合 foreach (var item in allCollection) { List<int> oneCollection = new List<int>(); rtn.Add(oneCollection); for (int i = 0; i < item.Length; i++) { int index = int.Parse(item[i].ToString()); if (index == 1) { oneCollection.Add(i); } } } return rtn; } static string GetNextNum(string num, string digitRadix) { StringBuilder rtn = new StringBuilder(); bool isNextAdd = false; for (int i = 0; i < num.Count(); i++) { int thisDigit = int.Parse(num[i].ToString()); if ((i == 0) || (isNextAdd == true)) { thisDigit++; int thisRadix = int.Parse(digitRadix[i].ToString()); if (thisDigit >= thisRadix) { isNextAdd = true; thisDigit = 0; } else { isNextAdd = false; } } rtn.Append(thisDigit); } return rtn.ToString(); } static List<List<int>> GetOneListIndex2(int digit) { List<List<int>> rtn = new List<List<int>>(); //获得位数的字符串“222” List<int> digitRadix = new List<int>(); List<int> initNum = new List<int>(); for (int i = 0; i < digit; i++) { digitRadix.Add(2); initNum.Add(0); } //获得所有 数字的集合 List<List<int>> allCollection = new List<List<int>>(); bool isLast = false; while (true) { initNum = GetNextNum(initNum, digitRadix, ref isLast); if (isLast == true) break; allCollection.Add(initNum); } //获得所有的组合 foreach (List<int> item in allCollection) { List<int> oneCollection = new List<int>(); rtn.Add(oneCollection); for (int i = 0; i < item.Count; i++) { if (item[i] == 1) { oneCollection.Add(i); } } } return rtn; } static List<int> GetNextNum(List<int> num, List<int> digitRadix, ref bool isLast) { List<int> rtn = new List<int>(); bool isNextAdd = false; for (int i = 0; i < num.Count(); i++) { int thisDigit = num[i]; if ((i == 0) || (isNextAdd == true)) { thisDigit++; if (thisDigit >= digitRadix[i]) { isNextAdd = true; thisDigit = 0; } else { isNextAdd = false; } } if ((i == num.Count() - 1) && (isNextAdd == true)) { isLast = true; } rtn.Add(thisDigit); } return rtn; }