ListBox内有N个项目,这个是不固定的。比如有3个项目 “青菜”,“紫菜”,“白菜”。 我想要的结果是 每个项目要这样配搭 每次要10个项目组合 比如 “青菜”10个 这是一个 “青菜”9 + “紫菜”1 10个 “青菜” 8 + “紫菜” 1 + “白菜” 1 =10 每个项目都要这样循环 应该有重复的结果,能考虑再内更好! 谢谢

解决方案 »

  1.   

    不就是 a个青菜,b个紫菜,c个白菜, 要求a+b+c=10的 a、b、c组合吗?这比分硬币那个简单多了
      

  2.   

    A= 0 TO 10
      B= 0 TO 10
        C= 0 TO 10
           IF A+B+C=10 THEN
             DEBUG.PRINT A & "+" & B & "+" & C & "=" & (A+B+C)
             EXIT FOR
           END IF
        NEXT
      NEXT
    NEXT3从循环,满足A+B+C=10即可
      

  3.   

    楼上的程序可以改一下,,循环未必只有三层,,说不定又有什么菜出来呢 ^_^而且可以判断 a+b+c...>0的时候 退出循环
      

  4.   

    Option ExplicitPrivate Sub Command1_Click()
        Dim str As String
        Dim strarr() As String
        Dim lens() As Integer
        Dim leng As Integer
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        
        'Init
        leng = 10
        str = "青菜,紫菜,白菜"
        strarr = Split(str, ",")
        ReDim lens(UBound(strarr))
        
        lens(0) = leng
        For i = 1 To UBound(lens)
            lens(i) = 0
        Next
        
        Text1.Text = ""
        
        'Start
        Do
            For i = 0 To UBound(strarr)
                Text1.Text = Text1.Text & strarr(i) & " " & lens(i) & " ,"
            Next
            Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) & " ;" & vbCrLf
            
            For i = UBound(lens) To LBound(lens) Step -1
                If lens(i) = 0 Then
                    
                Else
                    If i < UBound(lens) Then
                        lens(i) = lens(i) - 1
                        lens(i + 1) = lens(i + 1) + 1
                        
                        For j = i + 2 To UBound(lens)
                            lens(i + 1) = lens(i + 1) + lens(j)
                            lens(j) = 0
                        Next
                    Else
                        For j = UBound(lens) - 1 To LBound(lens) Step -1
                            If lens(j) > 0 Then
                                lens(j) = lens(j) - 1
                                lens(j + 1) = lens(j + 1) + 1
                                
                                For k = j + 2 To UBound(lens)
                                    lens(j + 1) = lens(j + 1) + lens(k)
                                    lens(k) = 0
                                Next
                                
                                Exit For
                            End If
                        Next
                    End If
                    
                    Exit For
                End If
            Next        If lens(UBound(lens)) = leng Then Exit Do
            
            'DoEvents
        Loop
        
        For i = 0 To UBound(strarr)
            Text1.Text = Text1.Text & strarr(i) & " " & lens(i) & " ,"
        Next
        Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) & " ;" & vbCrLfEnd Sub