A simple algorithm follows from this reasoning:
- You can build at most as many figures of type 1 as your budget allows;
- Once you've chosen the number n1 of figures of type 1, you can only buy a number of figures of type 2 going from 0 to the maximum allowed by your budget (minus what you spent on type 1);
- Once you've chosen the number n1 and n2 of figures of type 1 and 2, you can only buy a number of figures of type 3 going from 0 to the maximum allowed by your budget (minus what you spent on type 1 and 2);
- Same as above for type 4 to the last
As a simple sloppy example, if you have only three types:
$p1 = ... # Price of figures of type 1
$p2 = ... # Price of figures of type 2
$p3 = ... # Price of figures of type 3
$budget = ... # Total budget
for (my $i1=0; $i1 * $p1 <= $budget; $i1++) {
for (my $i2=0; $i2 * $p2 <= $budget - $i1 * $p1; $i2++) {
for (my $i3=0; $i3 * $p3 <= $budget - $i1 * $p1 - $i2 * $p2; $
+i3++) {
print "You can buy $i1 type 1 + $i2 type 2 + $i3 type 3\n"
+;
}
}
}
Of course you should never code it this way and should instead be using arrays and an iteration which lends itself better to extensions. Anyway, I hope the algorithm is clear.
Cheers
Antonio
Update: I have taken for granted that you could use more than one figure of each type. If that assumption is not valid, please disregard the solution or add the additional condition that all indices ($i1, $i2 ...) should not exceed 1.
The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.