Solved

Adding/Subtracting to 100

Posted on 2003-11-25
6
370 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

772 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