如何将几个数字进行搭配相加,得出全排列组合。

jock81890 2012-07-04 04:28:05
比如我这里有4个数字:2、3、4、6

我需要用随便几个数字搭配相加得出结果为全排列若干组合,请问如何写这样的代码?

2+3=5
2+4=6
2+6=12
2+3+4=9
2+3+4+6=15
2+3+6=11
2+4+6=12

3+4=7
3+6=9
3+4+6=13

4+6=10
...全文
503 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
熙风 2012-07-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

happy09li:
我要的是数值相加的组合,而不是把它当成字符串的组合。


Return_false:
错误“System.Array”并不包含“Sum”的定义



lizhibin11:
感觉不是c#代码啊,在我这都编译通不过。
[/Quote]

你再自己相加一下不就得了,,我不是说叫你稍微改下吗?
enaking 2012-07-04
  • 打赏
  • 举报
回复

List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = nlist[j].ToList();
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
List<List<int>> m = nlist.Where(p => p.Count > 1).ToList();

enaking 2012-07-04
  • 打赏
  • 举报
回复

List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = nlist[j].ToList();
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
List<List<int>> m = nlist.Where(p => p.Count > 1).ToList();

lizhibin11 2012-07-04
  • 打赏
  • 举报
回复
这个能不能编译?

static void Main(string[] args)
{
List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = new List<int>();
xlist.AddRange(nlist[j]);
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
for (int i = 0; i < nlist.Count; i++)
{
if (nlist[i].Count > 1)
continue;
nlist.RemoveAt(i);
i--;
}

//测试输出
for (int i = 0; i < nlist.Count; i++)
{
int m = 0;
for (int j = 0; j < nlist[i].Count; j++)
{
if (j == nlist[i].Count - 1)
Console.Write("{0} = ", nlist[i][j]);
else
Console.Write("{0} + ", nlist[i][j]);
m += nlist[i][j];
}
Console.Write(m);
Console.WriteLine();
}
Console.ReadLine();
}
jock81890 2012-07-04
  • 打赏
  • 举报
回复
happy09li:
我要的是数值相加的组合,而不是把它当成字符串的组合。


Return_false:
错误“System.Array”并不包含“Sum”的定义



lizhibin11:
感觉不是c#代码啊,在我这都编译通不过。
lizhibin11 2012-07-04
  • 打赏
  • 举报
回复
测试输出

for (int i = 0; i < m.Count; i++)
{
for (int j = 0; j < m[i].Count; j++)
{
if (j == m[i].Count - 1)
Console.Write("{0} = ", m[i][j]);
else
Console.Write("{0} + ", m[i][j]);
}
Console.Write(m[i].Sum());
Console.WriteLine();
}
Console.ReadLine();
lizhibin11 2012-07-04
  • 打赏
  • 举报
回复

List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = nlist[j].ToList();
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
List<List<int>> m = nlist.Where(p => p.Count > 1).ToList();
  • 打赏
  • 举报
回复
可以引入排列组合类http://kb.cnblogs.com/a/1652161/

然后调用代码
int[] arr = new int[] { 2, 3, 4, 6 };
for (int i = 2; i <= arr.Length; i++)
{
List<int[]> lst_Combination = Algorithms.PermutationAndCombination<int>.GetCombination(arr, i);
StringBuilder sb = new StringBuilder();
for (int k = 0; k < lst_Combination.Count; k++)
{
for (int j = 0; j < i; j++)
{
sb.Append(lst_Combination[k][j]);
if (j != i - 1)
sb.Append("+");
else
sb.Append("=");
}
sb.Append(lst_Combination[k].Sum());
sb.Append("\n");
}
Console.Write(sb.ToString());
}


熙风 2012-07-04
  • 打赏
  • 举报
回复


自己稍微做下修改

string[] Sample = new string[] { "2", "3", "4", "6"};
List<string> SampleList = new List<string>();
SampleList.AddRange(Sample);

List<string> result = getCombination(SampleList, 2);

private List<string> getCombination(List<string> SampleList, int m)
{
if (m == 1)
{
return SampleList;
}
List<string> result = new List<string>();
if (SampleList.Count == m)
{
StringBuilder temp_sb = new StringBuilder();
foreach (string s in SampleList)
{
temp_sb.Append(s);
}
result.Add(temp_sb.ToString());
Console.WriteLine(temp_sb.ToString());
return result;
}
string temp_firstelement = SampleList[0];
SampleList.RemoveAt(0);
List<string> temp_samplist1 = new List<string>();
temp_samplist1.AddRange(SampleList);
List<string> temp_list1 = getCombination(temp_samplist1, m - 1);
foreach (string s in temp_list1)
{
result.Add(temp_firstelement + s);
Console.WriteLine(temp_firstelement + s);
}
List<string> temp_samplist2 = new List<string>();
temp_samplist2.AddRange(SampleList);
List<string> temp_list2 = getCombination(temp_samplist2, m);
result.AddRange(temp_list2);
return result;
}
E次奥 2012-07-04
  • 打赏
  • 举报
回复
不懂意思!什么是全排列若干组合!

110,538

社区成员

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

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

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