如果提供1,2,3,4,5 这时 如果提供6 程序能给出所有求和可能。
6=1+5
6=2+4
小弟水平很低,求教各位高手。

解决方案 »

  1.   

    版主的那个,跟楼主的需求好象不一样呢。Option ExplicitPrivate aParam() As Long
    Private aUsed()  As Long
    Private mValue   As Long
    Private mTemp    As Long
    Private mDatSUM  As LongPrivate Sub GetNum(ByVal nBase As Long)
       Dim i&
       For i = nBase To mDatSUM
          If (aUsed(i) = 0) Then Exit For
       Next
       mTemp = mTemp + aParam(i)
       aUsed(i) = 1: nBase = nBase + 1
       If (mTemp >= mValue) Then Exit Sub
       If (nBase >= mDatSUM) Then Exit Sub
       Call GetNum(nBase)
    End SubPrivate Sub Command1_Click()
       Dim i&, j&, k&, sOut$
       
       mDatSUM = 8
       mValue = 10
       'mDatSUM = 5       ' 5个数
       'mValue = 6        ' 求和为6
       ReDim aParam(mDatSUM), aUsed(mDatSUM)
       For i = 1 To mDatSUM
          aParam(i) = i
       Next
       ' 要快递归速度,需要对aParam() 从小到大排序
       For i = 1 To mDatSUM - 1
          aUsed(i) = 1
          For k = i + 1 To mDatSUM
             mTemp = aParam(i)
             For j = k To mDatSUM
                aUsed(j) = 0
             Next
             Call GetNum(k)
             If (mTemp = mValue) Then
                sOut = mValue & " = "
                For j = i To mDatSUM
                   If (aUsed(j) = 1) Then sOut = sOut & aParam(j) & "+"
                Next
                Me.Print Left$(sOut, Len(sOut) - 1)
             End If
             aUsed(k) = 0
          Next
       Next
    End Sub
      

  2.   

    那简单
    x >= seed.LastOrDefault()
    改成
    x > seed.LastOrDefault()
    即可。
      

  3.   

    这个应该完整枚举了。
    Option ExplicitPrivate aParam()  As Long
    Private aUsed()   As Long
    Private mDatSUM   As Long
    Private mValue    As LongPrivate Sub GetNum(ByVal V&, ByVal nBase As Long, ByVal N As Long)
       Dim i&, t&, sOut$
       t = V - aParam(nBase)
       If (t < 0) Then Exit Sub
       If (N = 1) Then
          If (t = 0) Then
             aUsed(nBase) = 1
             sOut = mValue & " = "
             For i = 1 To nBase
                If (aUsed(i) = 1) Then sOut = sOut & aParam(i) & " + "
             Next
             aUsed(nBase) = 0
             Debug.Print Left$(sOut, Len(sOut) - 3)
          Else
             If (nBase < mDatSUM) Then
                i = nBase + 1
                Call GetNum(V, i, 1)
                Call GetNum(V, i, 0)
             End If
          End If
       Else
          i = nBase + 1
          If (i > mDatSUM) Then Exit Sub
          aUsed(nBase) = 1
          Call GetNum(t, i, 1)
          Call GetNum(t, i, 0)
          aUsed(nBase) = 0
       End If
    End SubPrivate Sub Command1_Click()
       Dim i&, t&
       
       mDatSUM = 15      ' 15个数(1~15)
       mValue = 30       ' 和为30
       'mDatSUM = 5       ' 5个数
       'mValue = 6        ' 求和为6
       ReDim aParam(mDatSUM), aUsed(mDatSUM)
       For i = 1 To mDatSUM
          aParam(i) = i
       Next
       ' 必要对 aParam() 从小到大排序
       For i = 1 To mDatSUM - 1
          aUsed(i) = 1
          t = mValue - aParam(i)
          Call GetNum(t, i + 1, 1)
          Call GetNum(t, i + 1, 0)
          aUsed(i) = 0
       Next
    End Sub
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var result = OutPut(6, new int[] { 1, 2, 3, 4, 5 }, new List<int>());
                foreach (var item in result)
                    Console.WriteLine(string.Join(",", item.Select(x => x.ToString())));
            }        public static IEnumerable<List<int>> OutPut(int sum, int[] array, IEnumerable<int> seed)
            {
                if (seed.Sum() == sum)
                    yield return seed.ToList();
                else
                    foreach (var item in array.Where(x => x > seed.LastOrDefault()).Select(x => seed.Concat(new int[] { x })).Where(x => x.Sum() <= sum).SelectMany(x => OutPut(sum, array, x)))
                        yield return item;        }
        }
    }
    看看C#简单不简单。1,2,3
    1,5
    2,4
    Press any key to continue . . .下面是1~15,sum=30的using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var result = OutPut(30, Enumerable.Range(1, 15).ToArray(), new List<int>());
                foreach (var item in result)
                    Console.WriteLine(string.Join(",", item.Select(x => x.ToString())));
            }        public static IEnumerable<List<int>> OutPut(int sum, int[] array, IEnumerable<int> seed)
            {
                if (seed.Sum() == sum)
                    yield return seed.ToList();
                else
                    foreach (var item in array.Where(x => x > seed.LastOrDefault()).Select(x => seed.Concat(new int[] { x })).Where(x => x.Sum() <= sum).SelectMany(x => OutPut(sum, array, x)))
                        yield return item;        }
        }
    }1,2,3,4,5,6,9
    1,2,3,4,5,7,8
    1,2,3,4,5,15
    1,2,3,4,6,14
    1,2,3,4,7,13
    1,2,3,4,8,12
    1,2,3,4,9,11
    1,2,3,5,6,13
    1,2,3,5,7,12
    1,2,3,5,8,11
    1,2,3,5,9,10
    1,2,3,6,7,11
    1,2,3,6,8,10
    1,2,3,7,8,9
    1,2,3,9,15
    1,2,3,10,14
    1,2,3,11,13
    1,2,4,5,6,12
    1,2,4,5,7,11
    1,2,4,5,8,10
    1,2,4,6,7,10
    1,2,4,6,8,9
    1,2,4,8,15
    1,2,4,9,14
    1,2,4,10,13
    1,2,4,11,12
    1,2,5,6,7,9
    1,2,5,7,15
    1,2,5,8,14
    1,2,5,9,13
    1,2,5,10,12
    1,2,6,7,14
    1,2,6,8,13
    1,2,6,9,12
    1,2,6,10,11
    1,2,7,8,12
    1,2,7,9,11
    1,2,8,9,10
    1,2,12,15
    1,2,13,14
    1,3,4,5,6,11
    1,3,4,5,7,10
    1,3,4,5,8,9
    1,3,4,6,7,9
    1,3,4,7,15
    1,3,4,8,14
    1,3,4,9,13
    1,3,4,10,12
    1,3,5,6,7,8
    1,3,5,6,15
    1,3,5,7,14
    1,3,5,8,13
    1,3,5,9,12
    1,3,5,10,11
    1,3,6,7,13
    1,3,6,8,12
    1,3,6,9,11
    1,3,7,8,11
    1,3,7,9,10
    1,3,11,15
    1,3,12,14
    1,4,5,6,14
    1,4,5,7,13
    1,4,5,8,12
    1,4,5,9,11
    1,4,6,7,12
    1,4,6,8,11
    1,4,6,9,10
    1,4,7,8,10
    1,4,10,15
    1,4,11,14
    1,4,12,13
    1,5,6,7,11
    1,5,6,8,10
    1,5,7,8,9
    1,5,9,15
    1,5,10,14
    1,5,11,13
    1,6,8,15
    1,6,9,14
    1,6,10,13
    1,6,11,12
    1,7,8,14
    1,7,9,13
    1,7,10,12
    1,8,9,12
    1,8,10,11
    1,14,15
    2,3,4,5,6,10
    2,3,4,5,7,9
    2,3,4,6,7,8
    2,3,4,6,15
    2,3,4,7,14
    2,3,4,8,13
    2,3,4,9,12
    2,3,4,10,11
    2,3,5,6,14
    2,3,5,7,13
    2,3,5,8,12
    2,3,5,9,11
    2,3,6,7,12
    2,3,6,8,11
    2,3,6,9,10
    2,3,7,8,10
    2,3,10,15
    2,3,11,14
    2,3,12,13
    2,4,5,6,13
    2,4,5,7,12
    2,4,5,8,11
    2,4,5,9,10
    2,4,6,7,11
    2,4,6,8,10
    2,4,7,8,9
    2,4,9,15
    2,4,10,14
    2,4,11,13
    2,5,6,7,10
    2,5,6,8,9
    2,5,8,15
    2,5,9,14
    2,5,10,13
    2,5,11,12
    2,6,7,15
    2,6,8,14
    2,6,9,13
    2,6,10,12
    2,7,8,13
    2,7,9,12
    2,7,10,11
    2,8,9,11
    2,13,15
    3,4,5,6,12
    3,4,5,7,11
    3,4,5,8,10
    3,4,6,7,10
    3,4,6,8,9
    3,4,8,15
    3,4,9,14
    3,4,10,13
    3,4,11,12
    3,5,6,7,9
    3,5,7,15
    3,5,8,14
    3,5,9,13
    3,5,10,12
    3,6,7,14
    3,6,8,13
    3,6,9,12
    3,6,10,11
    3,7,8,12
    3,7,9,11
    3,8,9,10
    3,12,15
    3,13,14
    4,5,6,7,8
    4,5,6,15
    4,5,7,14
    4,5,8,13
    4,5,9,12
    4,5,10,11
    4,6,7,13
    4,6,8,12
    4,6,9,11
    4,7,8,11
    4,7,9,10
    4,11,15
    4,12,14
    5,6,7,12
    5,6,8,11
    5,6,9,10
    5,7,8,10
    5,10,15
    5,11,14
    5,12,13
    6,7,8,9
    6,9,15
    6,10,14
    6,11,13
    7,8,15
    7,9,14
    7,10,13
    7,11,12
    8,9,13
    8,10,12
    9,10,11
    Press any key to continue . . .