Gina has $4.00, and wants to spend as much of her money as she can on candy. What is the maximum amount she can spend if she can only buy as many as she wants of the following:
Note: Gina doesn't care what kind of candy she buys, she just wants to spend as much of her money as possible.
If you liked this problem on Integer Programming, try Part 2 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
I actually just used my mind in solving lol
guys whts the difference between python and c++
Python and C++ are programming languages with quite different targets and use area.Python is interpreted while C++ is compiled (in most cases) , Python is generally used in prototyping , small programs , scripts , plugins , server side scripting etc. while C++ is generally used for systems programming , games (most AAA games) etc.
Guy's can you please tell me why do we use loops ? whats the purpose
Loops are used for 'repeating' a certain number of statements for a specified number of times. Loops are used in places where you need to repeat some part of the code more than once.
Small example in C++:
If you wrote a program that wants to print 100 *'s on the console screen, you'd need to repeat the statement that prints it.
for(int I = 1; I <= 100; I++)
std::cout << "*";
So, in a nutshell, loops are used in programs to iterate a block of statements as per as the conditions of flow of control of the program(I know this sounds quite technical).
Here are a few links to help you out: loops , flow of control
Although the scale of the problem allows for heuristics such as those given in the other answers to be applied to arrive at an educated guess, the problem admits a correct solution via dynamic programming. Since once is not able to arrive at any valid greedy methods to maximize utility in this problem, one has to rely on a weak structure present in the problem that is informally given below
"The maximum expenditure must be incurred by first spending money on buying one unit of licorice, drops or bears and then spending the rest of the money most effectively".
Thus, one goes over all (3) possible choices of the first candy to buy
Candy bought (Money remaining)
Now one recursively finds the best ways one can spend $3.41, $3.21 and $3.05. Let those numbers be $x, $y and $z (i.e. given $3.41, I can at best spend $x out of it and so on), then the final answer can simply be arrived at by taking max ( 3 . 4 1 + x , 3 . 2 1 + y , 3 . 0 5 + z )
Now although there is scope for improving the recursive solution by memoization techniques, this is not required for this toy problem. The final solution is $3.95 which can be spent by buying 5 drops.
using my first script today : not pretty well and slow, but it's ok
Private Sub test()
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim iOk As Integer, jOk As Integer, kOk As Integer, lOk As Integer
Dim a As Integer, b As Integer, c As Integer, d As Integer
Dim max As Integer = 400
Dim ok(4) As Integer
Dim calc As Integer = 0, highest As Integer = 0
a = 59
b = 79
c = 95
d = 10000
For i = 0 To 1 + Math.Round(max / a)
For j = 0 To 1 + Math.Round(max / b)
For k = 0 To 1 + Math.Round(max / c)
For l = 0 To 1 + Math.Round(max / d)
calc = a * i + b * j + c * k + d * l
If calc <= max AndAlso calc > highest Then
iOk = i
jOk = j
kOk = k
lOk = l
highest = calc
End If
Next
Next
Next
Next
MessageBox.Show(String.Format("{0}:{1}:{2}:{3}={4}", iOk, jOk, kOk, lOk, highest))
End Sub
A simple solution in C++ :
int main()
{
float limit = 4 , costA = 0.59 , costB = 0.79 , costC = 0.95;
float maxCost = 0;
float totalCost = 0;
float x,y,z;
for(x = 0; costA*x < limit ; ++x)
{
for(y = 0 ; costA*x + costB*y < limit ; ++y)
{
for(z = 0 ; costA*x + costB*y + costC*z < limit ; ++z)
{
totalCost = costA*x + costB*y + costC*z;
if(totalCost < limit)
maxCost = std::max(maxCost,totalCost);
}
}
}
cout << maxCost << endl;
return 0;
}
Actually, if you observe the choices, you will find all of them are multiples of 5 in cents. Thus, if Gina was to buy licorice, she would have to buy a multiple of 5. The same goes for chocolate drops. So we have to consider some cases.
Case 1: She buy 5 licorice and gummy bears. She would have spend 3.90 dollars. Case 2: She buy 5 chocolate drops. She would have spent 3.95 dollars. Case 3: She buy 4 gummy bears. she would have spent 3.90 dollars.
Thus the maximum money she would have spent is 3.95 dollars.
i = X = Y = 0
while i <= 6:
j = 0
while j <= 5:
k = 0
while k <= 4:
X = i*0.59 + j*0.79 + k*0.95
if X > Y and X <= 4.00: Y = X
k = k + 1
j = j + 1
i = i + 1
print Y
>Here is a straightforward solution in C#.
>ar is an ArrayList (similar to a one-dimensional array)
>Findmax finds the maximum value from the ArrayList.
>There are 7 possible combinations
> 1 or 2 or 3 candies may be selected concurrently
ArrayList ar = new ArrayList();
double sm = 0;
//Loop 1
for (int i = 7; i >= 1; i--)
{
sm = i * 0.59;
if (sm > 4)
{
continue;
}
else
{
ar.Add(sm);
}
}
//Loop 2
for (int i = 7; i >= 1; i--)
{
sm = i * 0.79;
if (sm > 4)
{
continue;
}
else
{
ar.Add(sm);
}
}
//Loop 3
for (int i = 7; i >= 1; i--)
{
sm = i * 0.95;
if (sm > 4)
{
continue;
}
else
{
ar.Add(sm);
}
}
//Loop 4
for (int i = 7; i >= 1; i--)
{
for (int j = 7; j >= 1; j--)
{
sm = i * 0.59 + j * 0.79;
if (sm > 4)
{
continue;
}
else
{
ar.Add(sm);
}
}
}
//Loop 5
for (int i = 7; i >= 1; i--)
{
for (int j = 7; j >= 1; j--)
{
sm = i * 0.79 + j * 0.95;
if (sm > 4)
{
continue;
}
else
{
ar.Add(sm);
}
}
}
//Loop 6
for (int i = 7; i >= 1; i--)
{
for (int j = 7; j >= 1; j--)
{
sm = i * 0.59 + j * 0.95;
if (sm > 4)
{
continue;
}
else
{
ar.Add(sm);
}
}
}
//Loop 7
for (int i = 1; i < 7; i++)
{
for (int j = 1; j < 7; j++)
{
for (int k = 1; k < 7; k++)
{
sm = i * 0.59 + j * 0.79 + k * 0.95;
if (sm > 4)
{
continue;
}
else
{
ar.Add(sm);
}
}
}
}
MessageBox.Show(FindMax2(ar).ToString());
nice solution
There is actually a mathematical principle involved in my stating that there are 7 combinations. Mathematically (using the formula for nCr) it is : 3C1 + 3C2 + 3C3 = 3 + 3 + 1 = 7 i.e. the summation of combinations of 3 items taken 1 at a time + 3 items taken 2 at a time + 3 items taken 3 at a time
This is my python code: S=0.0 a=0.0 while(a<=4.0): b=0.0 while(b<=4.0): c=0.0 while(c<=4.0): T=a+b+c if(T>S and T<=4.0): S=T c=c+0.95 b=b+0.59 a=a+0.79 print 'Max is %.2f'%S
The choices are all divisible by 5 so the Licorice and Chocolate drops have to be bought in fives in order to make a multiple of 5. Note that 5 Chocolate drops cost 79x5=395,knocking out 380 and 385. We see that 400 in not a nonnegative linear combination of 59x5=295, 79x5=395, and 95. (This is because the smallest combination is 95+295=390 and the second smallest is 95+395=490.) Therefore the answer is 395. (All numbers are in cents.)
Simple !! My solution in python
list=[]
for i in range(8):
for j in range(4):
for k in range(4):
if 0.59*i+0.79*j+0.95*k<4:
list.append(0.59*i+0.79*j+0.95*k)
print max(list)
My solution is kinda verbose. At the end it shows the answer and the correct combination of the products required to buy. I used python for the solution. If anyone has anything to improve this I will appreciate.
import math
lic = 0.59
choc = 0.79
gum = 0.95
total = 4.00
def buyable (item):
return math.ceil(total/item)
d = {}
for i in range(buyable(choc)):
for j in range(buyable(candy)):
for k in range(buyable(gum)):
x = i*lic + j*choc + k*gum
if (x <= total and x >= (total - 1)):
print("{} Lic, {} Choc, {} Gum : Total = {}".format(i, j, k, x))
prep_val = "{} lic, {} choc, {} gum".format(i, j, k)
d[x] = prep_val
k += 1
j += 1
i += 1
print("Answer")
print(max(d.keys()))
print(d[max(d.keys())])
Running the Script:
0 Lic, 0 Choc, 4 Gum : Total = 3.8
0 Lic, 1 Choc, 3 Gum : Total = 3.6399999999999997
0 Lic, 2 Choc, 2 Gum : Total = 3.48
0 Lic, 3 Choc, 1 Gum : Total = 3.3200000000000003
0 Lic, 4 Choc, 0 Gum : Total = 3.16
0 Lic, 5 Choc, 0 Gum : Total = 3.95
1 Lic, 0 Choc, 3 Gum : Total = 3.4399999999999995
1 Lic, 1 Choc, 2 Gum : Total = 3.28
1 Lic, 2 Choc, 1 Gum : Total = 3.12
1 Lic, 3 Choc, 1 Gum : Total = 3.91
1 Lic, 4 Choc, 0 Gum : Total = 3.75
2 Lic, 0 Choc, 2 Gum : Total = 3.08
2 Lic, 1 Choc, 2 Gum : Total = 3.87
2 Lic, 2 Choc, 1 Gum : Total = 3.71
2 Lic, 3 Choc, 0 Gum : Total = 3.55
3 Lic, 0 Choc, 2 Gum : Total = 3.67
3 Lic, 1 Choc, 1 Gum : Total = 3.51
3 Lic, 2 Choc, 0 Gum : Total = 3.35
4 Lic, 0 Choc, 1 Gum : Total = 3.3099999999999996
4 Lic, 1 Choc, 0 Gum : Total = 3.15
4 Lic, 2 Choc, 0 Gum : Total = 3.94
5 Lic, 0 Choc, 1 Gum : Total = 3.8999999999999995
5 Lic, 1 Choc, 0 Gum : Total = 3.7399999999999998
Answer
3.95
0 lic, 5 choc, 0 gum
my perl script which approaches this is a general problem , by iterating over all possible choices and then adding them to the @cost array only if the total cost is less than total money #!/usr/bin/perl use warnings use strict; use List::Util qw[min max]; my $chocolate=0.79; my $licorice= 0.59; my $gummi= 0.95; my $money=4; my $maxcho=int($money/$chocolate); my $maxlic=int($money/$licorice); my $maxgummi=int($money/$gummi); my @cost; my $maxcandy=max($maxcho,$maxlic,$maxgummi); for my $i(0..$maxcandy) for my $j(0..$maxcandy){ for my $k(0..$maxcandy){ #push to the cost only if cost less than total money push @cost,($i $chocolate)+($j $licorice)+($k $gummi) if ($i $chocolate)+($j $licorice)+($k $gummi)<=$money; } } } print max(@cost),"\n";
the post didn't turn out so well here's a direct link : maxmoney.pl
public class Question1 { public static void main(String[] args) { double d; double bigger = 0; for (int i=0;i<6;i++) { for (int j=0;j<5;j++) { for(int k=0;k<4;k++) { d=(i .59)+(j 0.79)+(k*.95); if(d>bigger && d<4) { bigger=d; } } } } System.out.println("The maximum spend can be"+bigger); }
}
You can show $3.95 works with 5 chocolate drops. The only other choice that could possibly be correct is $4.00 but it can't work.
If Gina spends $4.00, she must either buy only gummy bears or 5 licorice/chocolate drops and the rest as gummy bears to make the money she spends divisible by 5 (buying 10 licorice/chocolate drops exceeds $4). If she buys only gummy bears, she will only spend $3.80. If she buys a mix of 5 licorice and chocolate drops, she will spend a value from $2.95 to $3.95, with increments of 20 cents. The only way she could buy gummy bears is when she spends $2.95, and adding 95 cents to that doesn't get as close to $4 as buying 5 chocolate drops for $3.95.
Nice. Your solution also shows one of the differences between programming and theoretical math. In theoretical math, we find a proof through various arguments that no combinations can lead to $4. Using programming, because none of the combinations lead to $4, we conclude that it is impossible (which in itself is also a proof).
Problem Loading...
Note Loading...
Set Loading...
Since this is a computer science problem, here's an algorithm I wrote in Python to solve this problem.
The programme returns 3.95 as the answer.