Solved

Adding/Subtracting to 100

Posted on 2003-11-25
6
360 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
6 Comments
 
LVL 48

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 100

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Accepted Solution

by:
crypticfigure earned 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 114
how to send memory stream from ics Client To ics server ? 11 81
Turning python script into an applet 12 104
Beginner to Unreal Engine 4 5 61
This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 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

16 Experts available now in Live!

Get 1:1 Help Now