Solved

Posted on 2003-11-25
Medium Priority
397 Views
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
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

LVL 49

Expert Comment

ID: 9822846

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

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

ID: 9824989
correct
0

Accepted Solution

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

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

ID: 9827518
wow thanks alot!
0

Expert Comment

ID: 9848094
np

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

## Featured Post

Question has a verified solution.

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

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 post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …
###### Suggested Courses
Course of the Month15 days, 2 hours left to enroll