• 主页
• C#综合技术
• C#互联网桌面应用
• AppLauncher
• WinForm
• WPF
• 问答

# 最近组合算法，探讨的多，问个问题

CutBug 2009-02-20 11:15:06

``````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();
}
}
}
}
}``````

08 07 06 05 04 03
08 07 06 05 04 02
08 07 06 05 03 02
08 07 06 04 03 02
08 07 05 04 03 02
08 06 05 04 03 02
07 06 05 04 03 02
08 07 06 05 04 01
08 07 06 05 03 01
08 07 06 04 03 01
08 07 05 04 03 01
08 06 05 04 03 01
07 06 05 04 03 01
08 07 06 05 02 01
08 07 06 04 02 01
08 07 05 04 02 01
08 06 05 04 02 01
07 06 05 04 02 01
08 07 06 03 02 01
08 07 05 03 02 01
08 06 05 03 02 01
07 06 05 03 02 01
08 07 04 03 02 01
08 06 04 03 02 01
07 06 04 03 02 01
08 05 04 03 02 01
07 05 04 03 02 01
06 05 04 03 02 01

...全文
919 点赞 收藏 35

35 条回复

sinostyle 2012-10-29

mark

``````
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)
{

//如果不进位
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]];
}
}
}
}
``````

LZ给出的算法,用的全是位运算,只是程序本身循环判断的数量太多了,所以影响了效率,算32取16,

min_jie给出的算法,本身属于挺经典的算法,效率也是很高的,但实际上是因为字符串操作的缘故拖累了算法的效率,

C_sdnElf 2009-02-22

LZ的这种认真劲确实很让人佩服,如果是输出到List<string>,我可以试着优化一下输出部分,

[Quote=引用 29 楼 CutBug 的回复:]

C# code//result = GetCombinationF1(set, m);//n=15 8ms n=20 about 130ms;
[/Quote]

weilong147247943 2009-02-22

cnming 2009-02-22

lnwuyaowei 2009-02-22

1.效率判断的方法不明确，没有考虑垃圾回收等问题存在，所以难以保证验证结果准确。
2.代码中直接console.write，这是非常影响效率的，所以实际运行速度要比各位的算法快得多。

mokton 2009-02-22
LG，学习。

yulien 2009-02-22
ding

CutBug 2009-02-22

CutBug 2009-02-22

``````//result = GetCombinationF1(set, m);//n=15 8ms n=20 about 130ms;

CutBug 2009-02-22
n=20时，litaoye的方法最快，我的方法居然比1L的方法还慢，判断二进制有几个1没找到好的方法
n=40时，litaoye的方法没有min_jie的递归方法快

CutBug 2009-02-22

GetCombinationF1为我的方法
GetCombinationF2为litaoye的方法
GetCombinationF3为1L的方法
GetCombinationF4为min_jie的方法

``````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++)
{
}
List<string> result;

GC.Collect();
Stopwatch watch = new Stopwatch();
using (new AutoWatch(watch))
{
//result = GetCombinationF1(set, m);//n=20 about 130ms;
}
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++)
{
}
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
{
}
}
}
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;
}
}

}
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";
}

//如果不进位
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 s;
while (start != string.Empty)
{
s = "";
for (int i = 0; i < len; i++)
if (start[i] == '1') s+=data[i] + "\t";
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();
}
}

}
}``````

[Quote=引用 17 楼 litaoye 的回复:]

LZ给出的算法,用的全是位运算,只是程序本身循环判断的数量太多了,所以影响了效率,算32取16,

min_jie给出的算法,本身属于挺经典的算法,效率也是很高的,但实际上是因为字符串操作的缘故拖累了算法的效率,

[/Quote]

birdlonger 2009-02-22
mark && up

http://topic.csdn.net/u/20090106/14/7e1d07fe-32f0-4753-853a-76772b05aaa7.html

``````
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;
}
}
}
``````

typeof 2009-02-21

C#

10.4w+

.NET技术 C#