数组中的可能组合

gycgjy 2008-09-01 02:35:53
有一数组,排过序的,数组个数不定。例如:
可能是:
int[] array = new int[]{1,2,3,4,5,6,7,8,9}
也可能是:
int[] array = new int[]{1,3,4,5,7,8,9}
以第一个数组为例,找出五个一组的所以组合,即:
1,2,3,4,5
1,2,3,4,6
1,2,3,4,7
.
.
.
5,6,7,8,9
...全文
119 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gycgjy 2008-09-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Macosx 的回复:]
我的有错吗
[/Quote]

没有错。但是我的思想就是ojlovecd写的方法。
我姓区不姓区 2008-09-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Macosx 的回复:]
我的有错吗
[/Quote]
ms楼主只需组合,没说要排列,你的是对的,而且执行效率也很高,我可能将问题复杂化了
Macosx 2008-09-01
  • 打赏
  • 举报
回复
我的有错吗
浮生若梦丶 2008-09-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ojlovecd 的回复:]
C# code
static void Main(string[] args)
{
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 };
List<string> list = new List<string>();
foreach (int i in array)
list.Add(i.ToString());
Perm(list, new List<string>(), array.Length - 5);
}
static void Perm(List<string> list, List<string> listr…
[/Quote]
jf
gycgjy 2008-09-01
  • 打赏
  • 举报
回复
谢谢ojlovecd,你的答案就是我想要的。
我姓区不姓区 2008-09-01
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 };
List<string> list = new List<string>();
foreach (int i in array)
list.Add(i.ToString());
Perm(list, new List<string>(), array.Length - 5);
}
static void Perm(List<string> list, List<string> listresult, int length)
{
if (list.Count == length)
{
StringBuilder sb = new StringBuilder();
foreach (string s in listresult)
sb.Append(s + " ");
Console.WriteLine(sb.ToString());
}
for (int i = 0; i < list.Count; i++)
{
List<string> listTemp = new List<string>();
for (int j = 0; j < list.Count; j++)
if (j != i)
listTemp.Add(list[j]);
List<string> listResultTemp = new List<string>();
for (int j = 0; j < listresult.Count; j++)
listResultTemp.Add(listresult[j]);
listResultTemp.Add(list[i]);
Perm(listTemp, listResultTemp, length);
}
}
}
Macosx 2008-09-01
  • 打赏
  • 举报
回复
using System;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = { 2, 3, 4, 5, 6};
OutputCombinatorial(a, 3);
}

private static void OutputCombinatorial(int[] array, int outCount)
{
int[] indexes = Enumerable.Range(0, outCount).ToArray();

int i = indexes.Length - 1;
while (i > -1)
{
OutputOneCombination(array, indexes);
i = indexes.Length - 1;
indexes[i]++;
bool reset = false;
while (indexes[i] == array.Length - outCount + i + 1)
{
reset = true;
i--;
if (i == -1)
{
break;
}
indexes[i]++;
}
if (i > -1 && reset)
{
for (int j = i + 1; j < outCount; j++)
{
indexes[j] = indexes[j - 1] + 1;
}
}
}
}

private static void OutputOneCombination(int[] array, int[] indexes)
{
for (int i = 0; i < indexes.Length; i++)
{
Console.Write(array[indexes[i]].ToString() + " ");
}
Console.WriteLine();
}
}
}


只用一处linq, 你改成用for初始化即可
LQknife 2008-09-01
  • 打赏
  • 举报
回复
最好先把这样的数组转化成List<Int>集合
xiaOdl 2008-09-01
  • 打赏
  • 举报
回复
上网找一下排列组合算法
gycgjy 2008-09-01
  • 打赏
  • 举报
回复
忘了说一句了,最好在vs2005中实现。俺还没有用过vs2008和.net framework 3.5

110,538

社区成员

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

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

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