Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

chocolate

Name: ecrofirT 2009-08-21 9:32

A man has to buy some chocolate boxes
there are different types of boxes:
weight (kg)/// income ($) that the man gets for each box sold
1               10
2               26
3               43
4               52
5               53
6               56
7               71
8               87
9               95
10             100

Due to legal restrictiohns he can only buy 20 boxes and no more than 100Kg total
Also, he wants to choose 2 sizes because it's easier to commercialize.

so I need to write a program in qb/c/java solving this problem

can you help me? just gimme some ideas or pseaudocode please

I think it has something to do with arrays [weight,income] for each type but I'm lost

Name: Anonymous 2013-05-18 17:49

#!/usr/bin/env python
boxes = [
    (1, 10),
    (2, 26),
    (3, 43),
    (4, 52),
    (5, 53),
    (6, 56),
    (7, 71),
    (8, 87),
    (9, 95),
    (10, 100),
]

best_set = None
best_weight = 0
best_income = 0
for i, box_a in enumerate(boxes):
    for box_b in boxes[:i] + boxes[i+1:]:
        for x in xrange(20):
            box_set = [box_a] * x + [box_b] * (20-x)
            total_weight = sum(w for w, n in box_set)
            total_income = sum(n for w, n in box_set)
            if total_weight <= 100 and total_income > best_income:
                best_set = box_set
                best_weight = total_weight
                best_income = total_income

box_a, box_b = list(set(best_set))
print 'best_set: %s x %d + %s x %d' % (
    box_a, best_set.count(box_a),
    box_b, best_set.count(box_b),
)
print 'total_weight: %dkg' % (best_weight)
print 'total_income: $%d' % (best_income)


Output:
best_set: (4, 52) x 15 + (8, 87) x 5
total_weight: 100kg
total_income: $1215

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List