Solved

greatest number of numbers that sums to n

Posted on 2004-08-07
2
175 Views
Last Modified: 2012-05-05
Hi.  I am trying to make a program that takes in a list box (lets call it list1) with a bunch of items in it that have a numerical value.  Next, I have 4 other numbers in a different list.  The point of this is to take a number from the list2 and display all the possibilities in which when you sum up that first card with any number of cards from list1, the total is exactly 11.

Can anyone make me a function that does this? Thank you.
0
Comment
Question by:cachedVB
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 80 total points
ID: 11745822
This creates two string arrays.
Indices will show the combination in terms of the list positions of the parts, e.g. "1, 3 ,4"
Values shows the component parts including the number from list2 e.g. "1 + 5 + 5"

Private Sub GetCombinations(List2Number as integer)
Dim Indices() As String
Dim Values() As String
Dim CombinationCount As Integer
Dim Total As Integer
Dim i As Integer
Dim j As Integer
Dim Value As String
Dim Index As String

For i = 1 To (2 ^ List1.ListCount)
    Value = List2Number
    Index = ""
    Total = List2Number
    For j = 0 To List1.ListCount - 1
        If 2 ^ j And i Then
            Total = Total + List1.List(j)
            Index = Index & IIf(Index = "", "", ", ") & j
            Value = Value & " + " & List1.List(j)
        End If
        Next j
    If Total = 11 Then
        ReDim Preserve Indices(CombinationCount)
        ReDim Preserve Values(CombinationCount)
        Indices(CombinationCount) = Index
        Values(CombinationCount) = Value
        CombinationCount = CombinationCount + 1
    End If
Next i
End Sub
0
 
LVL 4

Author Comment

by:cachedVB
ID: 11746340
Thanks! Just what I wanted... although I had to tweak it a bit to find the longest one. :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now