求.NET的一个排列组合

wenzi1 2012-06-11 10:53:19
如 1,2,3
要求输出 1,12,123,13,2,23,3
所有组合,不考虑输出顺序 不能有重复的数据。
...全文
300 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2012-06-11
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
ShowResult();
}

public static void ShowResult()
{
string str = "1,2,3";
string[] temp1 = str.Split(',');
List<string> list = new List<string>();
foreach (string s in temp1)
{
if (list.Count == 0)
list.AddRange(temp1);
else
list.AddRange(JoinPart(list, s));
}
foreach (string s in list)
Console.WriteLine(s);
}
public static List<string> JoinPart(List<string> part1, string s)
{
List<string> result = new List<string>();
foreach (string str1 in part1)
{
result.Add(str1 + s);
}

return result;
}
/*
1
2
3
12
22
32
13
23
33
123
223
323
*/
feng84131421 2012-06-11
  • 打赏
  • 举报
回复
1,2,3,4,5
第一遍:
12345=》15,25,35,45,14,24,34,13,23,13
第二遍:
25=》125
35=》135,235
45=》145,245,345
24=》124
34=》134,234
23=》123
第三遍:
235=》1235
245=》1245
345=》1345,2345
234=》1234
第四遍:
2345=》12345
最后:
15,25,35,45,14,24,34,13,23,13
125,135,235,145,245,345,124,134,234,123
1235,1245,1345,2345,1234
12345
ParanoidKing 2012-06-11
  • 打赏
  • 举报
回复
参考http://www.cnblogs.com/rogerwei/archive/2010/11/18/1880336.html

//-----------------------------------------------------------------------------
//
// 算法:排列组合类
//
// 版权所有(C) Snowdust
// 个人博客 http://blog.csdn.net/snowdust & http://snowdust.cnblogs.com
// MSN & Email snowdust77@sina.com
//
// 此源代码可免费用于各类软件(含商业软件)
// 允许对此代码的进一步修改与开发
// 但必须完整保留此版权信息
//
//-----------------------------------------------------------------------------

/// <summary>
/// 递归算法求数组的组合(私有成员)
/// </summary>
/// <param name="list">返回的范型</param>
/// <param name="t">所求数组</param>
/// <param name="n">辅助变量</param>
/// <param name="m">辅助变量</param>
/// <param name="b">辅助数组</param>
/// <param name="M">辅助变量M</param>
private static void GetCombination<T>(ref List<T[]> list, T[] t, int n, int m, int[] b, int M)
{
for (int i = n; i >= m; i--)
{
b[m - 1] = i - 1;
if (m > 1)
{
GetCombination(ref list, t, i - 1, m - 1, b, M);
}
else
{
if (list == null)
{
list = new List<T[]>();
}
T[] temp = new T[M];
for (int j = 0; j < b.Length; j++)
{
temp[j] = t[b[j]];
}
list.Add(temp);
}
}
}

/// <summary>
/// 求数组中n个元素的组合
/// </summary>
/// <param name="t">所求数组</param>
/// <param name="n">元素个数</param>
/// <returns>数组中n个元素的组合的范型</returns>
public static List<T[]> GetCombination<T>(T[] t, int n)
{
if (t.Length < n)
{
return null;
}
int[] temp = new int[n];
List<T[]> list = new List<T[]>();
GetCombination(ref list, t, t.Length, n, temp, n);
return list;
}

应用

int[] arr = {1,2,3};
List<int[]> result = new List<int[]>();
for(int i = 0; i < arr.Length; i++)
{
result.AddRange(GetCombination(arr, i+1));
}
// 输出
foreach(int[] varr in result)
{
foreach(int vnum in varr)
{
Console.Write(vnum.ToString() + " ");
}
Console.WriteLine("");
}
/*
3
2
1
2 3
1 3
1 2
1 2 3
*/
feng84131421 2012-06-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
引用 1 楼 的回复:

f(n) = 2*f(n-1)+1

f(1) = 1


之后直接for都可以写出来了。。。
[/Quote]
这里说的不对。。。
feng84131421 2012-06-11
  • 打赏
  • 举报
回复
1,2,3,4,5
第一遍:
12345=》15,25,35,45
第二遍:
15
25=》125
35=》135,235
45=》145,245,345
第三遍:
125
135
235=》1235
145
245=》1245
345=》1345,2345
第四遍:
1235
1245
1345
2345=》12345
最后得到:
15,25,35,45
125,135,235,145,245,345
1235,1245,1345,2345
12345
feng84131421 2012-06-11
  • 打赏
  • 举报
回复
从后第一个往前拼接,循环n-1遍,把结果保存到result。
再从后第二个往前拼接,循环n-2遍,把结果合并到result。
依次。。。。。
最后得到所有result。
pmars 2012-06-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

f(n) = 2*f(n-1)+1
[/Quote]
f(1) = 1


之后直接for都可以写出来了。。。
pmars 2012-06-11
  • 打赏
  • 举报
回复
f(n) = 2*f(n-1)+1
juner77617 2012-06-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

其实1楼给的公式也没错,非常正确。这里的f(n)就是组合数,但要将内容显示则是通过循环+二进制位运算,其效率是最高的算法,因为没有比位运算的效率更高的算法,其中只用到循环而没有用到递归。另外他这个f(n)的求解比较麻烦,永远是依赖上个值,意味着要递归求解,因此我改下算法,变为这样:f(n) = 2^n - 1,这样计算就方便多了。
下面给出最佳算法,速度非常快。
C# code

publ……
[/Quote]

这个用位操作的算法貌似真的很厉害
  • 打赏
  • 举报
回复
System.Collections.Generic.List<string> sum;
protected void Page_Load(object sender, EventArgs e)
{
sum = new System.Collections.Generic.List<string>();
System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>() { 1,2,3};
bind(list, new System.Collections.Generic.List<int>(), 0, 1);
foreach (string s in sum.OrderBy(t => t.Length))
Response.Write(s);
}
private void bind(System.Collections.Generic.List<int> list, System.Collections.Generic.List<int> source, int index, int count)
{
if (source.Count <= count)
{
printf(source);
}
for (int i = index; i < list.Count; i++)
{
if (source.Contains(list[i]))
continue;
source.Add(list[i]);
bind(list, source, i, count + 1);
source.Remove(list[i]);
}
}
private void printf(System.Collections.Generic.List<int> List)
{
for (int i = 0; i < List.Count - 1; i++)
if (List[i] >= List[i + 1])
return;
string str = "";
for (int i = 0; i < List.Count; i++)
{
str += List[i];
if (i < List.Count - 1)
{
str += ",";
}
}
str += "<br/>";
sum.Add(str);
}
wy811007 2012-06-11
  • 打赏
  • 举报
回复
膜拜算法帝 最愁就是写算法了 o(︶︿︶)o 唉
qldsrx 2012-06-11
  • 打赏
  • 举报
回复
其实1楼给的公式也没错,非常正确。这里的f(n)就是组合数,但要将内容显示则是通过循环+二进制位运算,其效率是最高的算法,因为没有比位运算的效率更高的算法,其中只用到循环而没有用到递归。另外他这个f(n)的求解比较麻烦,永远是依赖上个值,意味着要递归求解,因此我改下算法,变为这样:f(n) = 2^n - 1,这样计算就方便多了。
下面给出最佳算法,速度非常快。

public static List<List<T>> GetCombination<T>(T[] t)
{
List<List<T>> result = new List<List<T>>();
int length = t.Length;
int num = (int)Math.Pow(2, length) - 1;
for (int i = 1; i <= num; i++)
{
List<T> tmp = new List<T>();
for (int j = 0; j < length; j++)
{
if ((i >> j) % 2 == 1)
tmp.Add(t[j]);
}
result.Add(tmp);
}
return result;
}
static void Main(string[] args)
{
int[] arr = { 1, 2, 3 };
List<List<int>> result = GetCombination(arr);
foreach (List<int> varr in result)
{
foreach (int vnum in varr)
{
Console.Write(vnum.ToString() + " ");
}
}
}
/*
输出
1
2
1 2
3
1 3
2 3
1 2 3
*/
a12321321321312321 2012-06-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

楼上的,你们什么眼神,这里只有6楼给的是最佳答案,而8楼给的直接是错误的算法,从输出内容就看得出来,不是组合而是排列,出现了重复“23”和“32”重复。
[/Quote]

楼上的眼睛看勋章去了,呵呵。
qldsrx 2012-06-11
  • 打赏
  • 举报
回复
楼上的,你们什么眼神,这里只有6楼给的是最佳答案,而8楼给的直接是错误的算法,从输出内容就看得出来,不是组合而是排列,出现了重复“23”和“32”重复。
Sheldon_Lou 2012-06-11
  • 打赏
  • 举报
回复
8楼+1
lx1988729 2012-06-11
  • 打赏
  • 举报
回复
学习了!八楼的应该是正解!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧