Tom and Gina are going hiking in the desert, and Tom is responsible for carrying as much water as he can with him. Unfortunately, however, he has only a few fixed sizes of water bottles into which he can put the water, and he can only carry 15.00 kg at most.
If his bottles have the following masses, and he can carry as many of each as he likes (so long as the total is less than or equal to 15 kg), what is the most amount of water that Tom can carry?
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.
x=0 y=0 z=0 w=0 R=0 for x in range (0,19) : for y in range (0,13) : for z in range (0,4) : for w in range(0,2): M = 0.77 x + 1.1 y + 3.4 z + 7 w if (R<M and M<=15) : R=M print "Max weight that can be carried is %s" % (R)
I made it in C. Take a look...
int main() { float x, bigger = 0; int k1, k2, k3, k4;
for(k1 = 0;k1 <= 19;k1++)
{
for(k2 = 0;k2 <= 13;k2++)
{
for(k3 = 0;k3 <= 4;k3++)
{
for(k4 = 0;k4 <= 2;k4++)
{
x = k1*0.77 + k2*1.10 + k3*3.4 + k4*7;
if( (x > bigger) && (x <=15)) bigger = x;
}
}
}
}
printf("%f", bigger);
}
If one tweaks around with the values given, then your method will become HIGHLY INEFFICIENT (for example, consider 0.07, 0.11, 0.13, 0.17 to be used to fill a 100 L tank. Your method will require at least a billion iterations, while it is possible to solve the problem in 10K iterations)
simple python code max =0
for i in range(0,19): for j in range(0,13): for k in range(0,4): for l in range(0,2): total = i 0.77+j 1.1+k 3.4+l 7 if (total>max) and (total<=15) : max = total
print max
this will give you the right answer, but the ranges should be +1, since this is computer science!
plz can u tell me what kind of standard this question is
Here is a solution i made in python. Have a look.
capacity = 0
capacities = [ ]
for a in xrange(0,15):
for b in xrange(0,15):
for c in xrange(0,15):
for d in xrange(0,15):
capacity = (a*0.77)+(b*1.10)+(c*3.4)+(d*7.0)
if capacity <=15.0:
capacities.append(capacity)
print max(prices)
It returns the value as 14.96.
/ C++ code /
void main() { float i,j,l,m; float val=0,max=0; for(i=0;i<20;i++) { for(j=0;j<14;j++) { for(l=0;l<5;l++) { for(m=0;m<3;m++) { val=i 0.77+j 1.10+l 3.4+m 7; if((val<=15)&&(val>max)) { max=val; } } } } } printf("max value=>%f",max); }
Solution in C++ (or C if you replace cout with stdio)
int main()
{
float lim = 15,maxcost = 0;
float costA = 7, costB = 3.4,costC = 1.1,costD = 0.77;
for(int a = 0; costA*a <= lim ; ++a)
{
for(int b = 0 ;costB*b <= lim-costA*a ; ++b)
{
for(int c = 0 ;costC*c <= lim-costA*a-costB*b ; ++c)
{
for(int d = 0 ; costD*d <= lim-costA*a-costB*b-costC*c ; ++d)
maxcost = std::max(maxcost,costA*a+costB*b+costC*c+costD*d);
}
}
}
cout << maxcost << endl;
}
Python Code:
list=[]
for i in range(15):
for j in range(15):
for k in range(5):
for l in range(3):
if 0.77*i+1.10*j+3.4*k+l*7<15.00:
list.append(0.77*i+1.10*j+3.4*k+l*7)
print max(list)
Wrote in Powershell :
First found the maximum possible number of each bottle (taken only one size of bottle at a time)
0.77 * 19 = 14.63 ----- <15 1.10 13 = 14.3 ----- <15 3.4 4 = 13.6 ----- <15 7*2 = 14 ----- <15
The code goes like this:
PS C:\Windows\system32> [float]$a1 = 0; [float]$a2 = 0; [float]$a3 = 0; [float]$a4 = 0; [float]$z = 0; [float]$largest = 0; for($a1 = 0;$a1 -le 19;$a1++) { for($a2 = 0;$a2 -le 13;$a2++) { for($a3 = 0;$a3 -le 4;$a3++) { for($a4 = 0;$a4 -le 2;$a4++) { $z = $a1 0.77 + $a2 1.10 + $a3 3.4 + $a4 7; if( ($z -gt $largest) -and ($z -le 15)) { $largest = $z; } } } } }
Write-host $largest
14.96
code in python
def d(n): i=x=y=0 while i<=19: j=0 while j<=13: k=0 while k<=4: m=0 while m<=2: x = 0.77 i+1.10 j+3.40 k+7.00 m if x>y and x<=n: y=x m=m+1 k=k+1 j=j+1 i=i+1 print y d(15.00)
c++ code for mingw.not efficient but I love recursive brute force.
btw divide solution by 100 ;-)
int mx=1500,t=77,s=110,m=340,l=700;
int findmax(int a){
int profit=a;
if(a<=mx){
if((a+t)<=mx){
profit = max(profit,findmax(a+t));
}
if((a+s)<=mx){
profit = max(profit,findmax(a+s));
}
if((a+m)<=mx){
profit = max(profit,findmax(a+m));
}
if((a+l)<=mx){
profit = max(profit,findmax(a+l));
}
return profit;
} else
return 0;
}
int main(){
cout<<findmax(0)<<endl;
}
Python Solution:
maxWater = 0
for a in range(int(15/0.77) + 2):
for b in range(int(15/1.1) + 2):
for c in range(int(15/3.4) + 2):
for d in range(int(15/7) + 2):
totalWater = 0.77*a + 1.1*b + 3.4*c + 7*d
if totalWater <= 15:
if totalWater > maxWater:
maxWater = totalWater
print maxWater
THIS IS A C# SOLUTION :
static void Main(string[] args) { double a = 7, b=3.4, c=1.1, d=.77; double n = 14.94;
for ( int i = 0 ; i < 20 ; i++)
{
for(int j =0 ; j <20 ; j++)
{
for (int k=0 ; k<20 ; k++)
{
for (int l=0 ; l<20 ; l++)
{
double x = a * i + b * j + c * k + d * l;
if (x > n && x<=15)
{
n = x;
}
}
}
}
}
Console.WriteLine(n);
Console.ReadLine();
The output is : 14.96
for i in range (0,10): for j in range(0,10): for k in range(0,10): for l in range(0,10): if tiny * i + small * j + medium *k + large *l <=15 and tiny * i + small * j + medium *k + large *l >=14: print i,j,k,l ,tiny * i + small * j + medium *k + large *l
then i appended the output in a list and sorted out and checked which was the highest value. The highest value was 14.96 for i=8, j=8, k= 0, l=0
#include<iostream.h>
int main()
{
int a,b,c,d;
float e,f;
cin>>f;
for(a=0;a<20;a++)
{
for(b=0;b<15;b++)
{
for(c=0;c<15;c++)
{
for(d=0;d<15;d++)
{
e=(a*0.77)+(b*1.1)+(c*3.4)+(d*7);
if(e==f)
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<d<<"\n\n";
}
}
}
}
return(0);
}
15 - no combination.
14.98 - no combination.
14.96 - two combinations.
14.94 - one combination.
Ask in comments if you have any doubts.
just for fun 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 = 1500 Dim ok(4) As Integer Dim calc As Integer = 0, highest As Integer = 0
a = 77
b = 110
c = 340
d = 700
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
Problem Loading...
Note Loading...
Set Loading...
This problem is a simpler version of the Knapsack problem . It can be solved recursively.Let C a p ( x ) be the maximum amount of water that can be carried for a weight limit of x . And let v be the set of the values of the smaller bottles.We can define recursively as C a p ( x ) = m a x ( { C a p ( x − v 0 ) + v 0 , C a p ( x − v 1 ) + v 1 , C a p ( x − v 2 ) + v 2 . . . , C a p ( x − v i ) + v i } )
We are basically subtracting each value of the set of bottle( v ) values from the limit( 1 5 k g ) and seeing which difference yields the maximum value..
This can be implemented easily in python..
Note:I multiplied each bottle value by 1 0 0 ,since it is easier to deal with integers.