|
-
Oct 21st, 2002, 03:26 PM
#1
Thread Starter
Member
Looping
i have 4 numbers ex(45,36,31,14)
and i want to put all the posiblities that is lower or equal ex(100)
so if the sum of it is lower or equal the program writes the qualification he solved
for example we start with number 45
100>45 so print "45"
45+45=90
100>90 so print "45 - 45"
45+45+45=135
100<135 so it goes to the next step
45+36=81
100>81 so print "45 - 36"
45+36+36=117
100<117 so it goes to the next step
45+36+31=112
100<112 so it goes to the next step
45+36+31+14=126
100<126 so it goes to the next step
45+36+14=95
100>95 so print " 45 - 36 14"
and i don't know what is the way to make a loop like this
and the result sould be like this
Code:
45
45 - 45
45 - 36
45 - 36 - 14
45 - 31
45 - 31 - 14
45 - 14
45 - 14 - 14
45 - 14 - 14 -14
36
36 - 36
36 - 36 - 14
36 - 36 - 14 - 14
36 - 31
36 - 31 - 31
36 - 31 - 14
36 - 31 - 14 - 14
36 - 14
36 - 14 - 14
36 - 14 - 14 - 14
36 - 14 - 14 - 14 - 14
31
31 - 31
31 - 31 - 31
31 - 31 - 14
31 - 31 - 14 - 14
31 - 14
31 - 14 - 14
31 - 14 - 14 - 14
31 - 14 - 14 - 14 - 14
14
14 - 14
14 - 14 - 14
14 - 14 - 14 - 14
14 - 14 - 14 - 14 - 14
14 - 14 - 14 - 14 - 14 - 14
14 - 14 - 14 - 14 - 14 - 14 - 14
and i tried and i've succeded to make all posiblities for no. 45
and all fo no.36 except
36 - 36 - 14
36 - 36 - 14 - 14
and it doesn't matter how the code is long or complecated all i want is to get the final result
and if there is any point u understand tell me and i'll explain what u want
thank u and good bye
-
Oct 21st, 2002, 04:09 PM
#2
Hyperactive Member
VB Code:
Private Function ret_Many(what As String, times As Integer) As String
Dim i As Integer, ret As String
ret = ""
For i = 1 To times
ret = ret + "- " + what
Next i
ret_Many = ret
End Function
Private Sub Form_Load()
Dim X() As Integer, times As Integer
ReDim X(1 To 4)
X(1) = 45
X(2) = 36
X(3) = 31
X(4) = 14
Text1.Text = ""
For i = 1 To 4
If i <> 1 Then Text1.Text = Text1.Text & vbNewLine
Text1.Text = Text1.Text & X(i)
For j = i To 4
Sum = X(i)
times = 0
While Sum < 100
times = times + 1
Sum = Sum + X(j)
If Sum < 100 Then
Text1.Text = Text1.Text & vbNewLine & X(i) & " " & ret_Many(Str(X(j)), times)
End If
Wend
Next j
Next i
End Sub
-
Oct 21st, 2002, 08:13 PM
#3
Thread Starter
Member
-
Oct 22nd, 2002, 03:14 PM
#4
Hyperactive Member
Ooops!
Slightly more complex than I thought . Will have a fix later, got to study for a semester test tomorrow though!
Cheers
-
Oct 22nd, 2002, 06:55 PM
#5
Thread Starter
Member
-
Oct 23rd, 2002, 06:02 AM
#6
-
Oct 23rd, 2002, 03:54 PM
#7
Thread Starter
Member
plz explain more
i don't understand what u mean
-
Oct 24th, 2002, 05:38 AM
#8
Fanatic Member
OK...
Firstly, have you heard of Permutations/Combinations?? (if not, just go to google and find out a bit about them)
What you need for this is all the permutations of 7 numbers made up of the 4 you have. (i.e. every way you can have 7 numbers, all of which are either 45,36,31 or 14. Then you can check these for the correct sums.
Now, the easiest way to get all possibilities like this is use a recursive sub:
VB Code:
Public Sub Recurse()
'The simplest recursive sub. Note that this, when called, will run
'for ever!!
Call Recurse
End Sub
Note that it calls itself. To make it useful for your purposes, change it a bit like this:
VB Code:
Public Sub Recurse(a as integer)
If a < 7 then
for i(a) = 1 to 4
numbers(a) = vals(i)
Call Recurse(a+1)
next i(a)
Else:
Bol = true
for k = 1 to 6
if numbers(k)>=numbers(k+1) then Bol = false
next k
If (Bol) then printthem()
end if
End Sub
I haven't checked this yet, so it probably isn't right.
Also, it isn't finished (you need other stuff, like a printthem() sub, and some public variables), but it's just the general idea.
sql_lall 
-
Oct 28th, 2002, 10:15 AM
#9
Thread Starter
Member
hey where r u guys
hi sql_lall sorry i didn't also understand u plz explain more
and the code didn't work
hi marnitzg where r u
anyone know how to make a loop to solve this problem
-
Oct 29th, 2002, 08:39 AM
#10
Hyperactive Member
Sorry, actually forgot about this . I don't like using recursive procedures (no particular reason just don't like them) but anyway decided to take sql_lall's advice so here it is. Sorry about not getting back sooner
VB Code:
Option Explicit
Private Nums(1 To 4)
Public Sub Recurse(ByVal Total As Integer, ByVal prnt As String, ByVal position As Integer)
Total = Total + Nums(position)
If Total <= 100 Then
Text1.Text = Text1.Text & prnt & vbNewLine
While position <= 4
Recurse Total, prnt & " - " & Trim(Str(Nums(position))), position
position = position + 1
Wend
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
Nums(1) = 45
Nums(2) = 36
Nums(3) = 31
Nums(4) = 14
Text1.Text = ""
For i = 1 To 4
Call Recurse(0, Trim(Str(Nums(i))), i)
Next i
End Sub
Hope that works now. A quick check looks like it works, but unfortunately thats what I thought last time
Cheers
Marnitz
-
Oct 29th, 2002, 02:34 PM
#11
Thread Starter
Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|