?
Solved

Adding/Subtracting to 100

Posted on 2003-11-25
6
Medium Priority
?
394 Views
Last Modified: 2010-04-17
I need to find out a way to figure out how to figure every way to get to 100
like
1+23-45+67-8+9 (thats not equal = 100)
but stuff like that.. in order.. allowing any amount of numbers.. 1-9 in that order
so like
1+2+3
12+3
1+23
123+
etc.. for both + and -...

i have no idea where to start from


to those who think this is a homework problem: it was a problem we had in class today, i spent along time figuring a correct answer and I just found it by trial and error and it took a long time. teacher said there was 12 different answers and im just wondering as to what they are
0
Comment
Question by:spearman123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 49

Expert Comment

by:dbrunton
ID: 9822846
That's a programming task.

Nice one though.  Very messy to do.

Generally its some sort of loop like

for i := 1 to 9 do
  for j := 2 to 8 do
       for K := 3 to 7 do
             { do something here and test the result.  If it's a 100 you've found an answer }

The for loops here have no relevance, they're just for show.

0
 
LVL 101

Expert Comment

by:mlmcc
ID: 9824970
An interesting problem.  Let me make sure I understand it correctly.

You put the digits

1   2   3   4   5    6    7    8     9


Now you can add as many + and - as you want between digits.  The digits cannot be swapped.  If you don't put one in then the two (or more) digits are put together.  Evaluate the expression to see if it is 100.

So
1 + 23 - 45 + 6 + 7 + 8 + 9 is legitimate though no 100
while
1 + 32 - 45 + 6 + 7 + 8 + 9 is not legitimate

mlmcc
0
 
LVL 1

Author Comment

by:spearman123
ID: 9824989
correct
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Accepted Solution

by:
crypticfigure earned 500 total points
ID: 9827361
output: ('+' starts and blank starts are the same)
123+45-67+8-9
123+4-5+67-89
123-45-67+89
123-4-5-6-7+8-9
12+3+4+5-6-7+89
12+3-4+5+67+8+9
12-3-4+5-6+7+89
1+23-4+56+7+8+9
1+23-4+5+6+78-9
1+2+34-5+67-8+9
1+2+3-4+5+6+78+9
+123+45-67+8-9
+123+4-5+67-89
+123-45-67+89
+123-4-5-6-7+8-9
+12+3+4+5-6-7+89
+12+3-4+5+67+8+9
+12-3-4+5-6+7+89
+1+23-4+56+7+8+9
+1+23-4+5+6+78-9
+1+2+34-5+67-8+9
+1+2+3-4+5+6+78+9
-1+2-3+4+5+6+78+9




Private Sub Command1_Click()

    startstring = "123456789"
    spaces = Len(startstring)
   
   
    '9 spaces before, each one can have a plus or minus, or nothing (joined) before it.
   

   
    On Error Resume Next
   
    Dim placedat() As Integer
    ReDim placedat(9)
   
   
    'set all to zero instead of undefined
    For a = 1 To 9
        placedat(a) = 0
    Next a
   
   
    '[(0-8)], each space having 1 through 3 now you go through each one:
    '000000000
    '000000001
    '000000002
    '000000010
    '000000011
   
   
    'endless loop
    While 1 = 1
   

        'add one to the end
        placedat(9) = placedat(9) + 1
       
        'go through each place and if it became a 3, then increment the one beside it
        'and set it to 0, as in 08 09 10 11 12
        For b = 1 To 9
        For a = 1 To 9
            If placedat(a) > 2 Then
           
            If a = 1 Then
                Exit Sub 'if you increment one left to the very last one, then you're done
            Else
                placedat(a - 1) = placedat(a - 1) + 1
                placedat(a) = 0
            End If
           
            End If
        Next a
        Next b
       
       
       
        'now you want to find the sum of all terms.
        'every time you get a full term, signalled by either the start of the
        'next term or getting to the end, then add that term to the main sum
        Dim outSum As Long
       
        outSum = 0
       
        Dim outTerm As String 'to find the term, you make a string var.
        outTerm = ""
        For a = 1 To 9 'go through each 'space'
       
       
       
           
           
            Select Case placedat(a)
           
            Case 0 'nothing
                'if nothing then add it to the term
                outTerm = outTerm & Mid(startstring, a, 1) '
            Case 1 '+
               
                outSum = outSum + CDbl(outTerm) 'add old term to final sum
                outTerm = "+" & Mid(startstring, a, 1) 'start a new term

            Case 2 '-
               
                outSum = outSum + CDbl(outTerm) 'add old term to final sum
                outTerm = "-" & Mid(startstring, a, 1) ' start a new term

            End Select
           
           
        Next a
       
        outSum = outSum + CDbl(outTerm) 'add the remaining term to final sum
       
       
        If outSum = 100 Then   'check if its 100 and print it out
            temp = ""
            For z = 1 To 9
                temp = temp & ct(placedat(z)) & Mid(startstring, z, 1)
            Next z
            PrintOut (temp & " ")
           
        End If
       
   
    Wend
   

End Sub


Public Function ct(num As Integer) As String
    ct = ""
    If num = 1 Then ct = "+"
    If num = 2 Then ct = "-"
End Function

Public Sub PrintOut(data As String)
   
    text1.Text = text1.Text & data & vbCrLf
End Sub
0
 
LVL 1

Author Comment

by:spearman123
ID: 9827518
wow thanks alot!
0
 

Expert Comment

by:crypticfigure
ID: 9848094
np

sorry bout the messy code tho... geeze I must have been intoxicated when I wrote that
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Progress
Introduction to Processes
Suggested Courses

741 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