比如我这里有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=123+4=7
3+6=9
3+4+6=134+6=10 

解决方案 »

  1.   

    自己稍微做下修改      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;
            }
      

  2.   

    可以引入排列组合类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());
                }
      

  3.   


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

  4.   

    测试输出            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();
      

  5.   

    happy09li:
     我要的是数值相加的组合,而不是把它当成字符串的组合。
    Return_false:
    错误“System.Array”并不包含“Sum”的定义lizhibin11:
    感觉不是c#代码啊,在我这都编译通不过。
      

  6.   

    这个能不能编译?        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();
            }
      

  7.   


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

  8.   


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