Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Knapsack-problem-0-1/QB64/knapsack-problem-0-1.qb64
Normal file
24
Task/Knapsack-problem-0-1/QB64/knapsack-problem-0-1.qb64
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
Open "knapsack.txt" For Output As #1
|
||||
N=7: L=5: a = 2^(N+1): Randomize Timer 'knapsack.bas DANILIN
|
||||
Dim L(N), C(N), j(N), q(a), q$(a), d(a)
|
||||
|
||||
For m=a-1 To (a-1)/2 Step -1: g=m: Do ' cipher 0-1
|
||||
q$(m)=LTrim$(Str$(g Mod 2))+q$(m)
|
||||
g=g\2: Loop Until g=0
|
||||
q$(m)=Mid$(q$(m), 2, Len(q$(m)))
|
||||
Next
|
||||
|
||||
For i=1 To N: L(i)=Int(Rnd*3+1) ' mass & cost
|
||||
C(i)=10+Int(Rnd*9): Print #1, i, L(i), C(i): Next ' origin
|
||||
|
||||
For h=a-1 To (a-1)/2 Step -1
|
||||
For k=1 To N: j(k)=Val(Mid$(q$(h), k, 1)) ' j() = cipher
|
||||
q(h)=q(h)+L(k)*j(k)*C(k) ' 0 or 1
|
||||
d(h)=d(h)+L(k)*j(k)
|
||||
Next
|
||||
If d(h) <= L Then Print #1, d(h), q(h), q$(h)
|
||||
Next
|
||||
max=0: m=1: For i=1 To a
|
||||
If d(i)<=L Then If q(i) > max Then max=q(i): m=i
|
||||
Next
|
||||
Print #1,: Print #1, d(m), q(m), q$(m): End
|
||||
Loading…
Add table
Add a link
Reference in a new issue