110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string str = "01 02 03 04 05 06 07 08";
string[] set = str.Split(' ');
int n = set.Length;
int m = 6;
int min = (0x01 << m) - 1;//00111111
int max = min << (n - m);//11111100
int j;
int k;
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)
{
k = 0x01;
for (int l = n-1; l>=0; l--)
{
if ((k & i) == k)
{
Console.Write(set[l] + "\t");
}
k <<=1;
}
Console.WriteLine();
}
}
}
}
}
static void createPerArray(string[] strArray, int selectCount)
{
int totalCount = strArray.Length;
int[] currentSelect = new int[selectCount];
int last = selectCount - 1;
int position = 0;
List<string> output = new List<string>();
string temp = "";
int position2 = 0;
//付初始值
for (int i = 0; i < selectCount; i++)
{
currentSelect[i] = i;
if(i < selectCount - 1)
temp += strArray[currentSelect[i]];
}
while (true)
{
output.Add(temp + strArray[currentSelect[last]]);
//如果不进位
if (currentSelect[last] < totalCount - 1)
currentSelect[last]++;
else
{
//进位部分
position = last;
position2 = temp.Length - strArray[currentSelect[position - 1]].Length;
while (currentSelect[position - 1] == currentSelect[position] - 1)
{
position--;
if (position == 0)
return;
position2 -= strArray[currentSelect[position - 1]].Length;
}
currentSelect[position - 1]++;
temp = temp.Remove(position2) + strArray[currentSelect[position - 1]];
for (int i = position; i < selectCount; i++)
{
currentSelect[i] = currentSelect[i - 1] + 1;
if (i < selectCount - 1)
temp += strArray[currentSelect[i]];
}
}
}
}
//result = GetCombinationF1(set, m);//n=15 8ms n=20 about 130ms;
//result = GetCombinationF2(set, m); //n=15 5ms,n=20 about 44ms, n=40 about 6s
//result = GetCombinationF3(set, m);//n=15,4ms,n=20 about 16ms n=40 about 2.5s
result = GetCombinationF4(set, m);//n=15 11ms,n=20 about 95ms n=40 about 13s
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int n = 40;
int m = 6;
string[] set = new string[n];
for (int i = 0; i < n; i++)
{
set[i] = i.ToString().PadLeft(2, '0');
}
List<string> result;
GC.Collect();
Stopwatch watch = new Stopwatch();
using (new AutoWatch(watch))
{
//result = GetCombinationF1(set, m);//n=20 about 130ms;
//result = GetCombinationF2(set, m); //n=20 about 44ms, n=40 about 6s
//result = GetCombinationF3(set, m);//n=20 about 16ms n=40 about 2.5s
result = GetCombinationF4(set, m);//n=20 about 95ms n=40 about 13s
}
Console.WriteLine(watch.ElapsedMilliseconds);
//print output
foreach (string s in result)
{
//Console.WriteLine(s);
}
}
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;
}
public sealed class AutoWatch : IDisposable
{
private Stopwatch watch;
public AutoWatch(Stopwatch watch)
{
this.watch = watch;
watch.Start();
}
void IDisposable.Dispose()
{
watch.Stop();
}
}
}
}
static void Main(string[] args)
{
createPerArray(new string[] { "00", "01", "02", "03", "04", "05", "06", "07" }, 6);
}
static void createPerArray(string[] strArray, int selectCount)
{
int totalCount = strArray.Length;
int[] currentSelect = new int[selectCount];
int last = selectCount - 1;
//付初始值
for (int i = 0; i < selectCount; i++)
currentSelect[i] = i;
while (true)
{
//输出部分,生成的时候从0计数,所以输出的时候+1
for (int i = 0; i < selectCount; i++)
Console.Write(strArray[currentSelect[i]]);
Console.WriteLine();
//如果不进位
if (currentSelect[last] < totalCount - 1)
currentSelect[last]++;
else
{
//进位部分
int position = last;
while (position > 0 && currentSelect[position - 1] == currentSelect[position] - 1)
position--;
if (position == 0)
return;
currentSelect[position - 1]++;
for (int i = position; i < selectCount; i++)
currentSelect[i] = currentSelect[i - 1] + 1;
}
}
}