Life And Death In The Desert

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?

  • Tiny: 0.77 kg
  • Small: 1.10 kg
  • Medium: 3.4 kg
  • Large: 7 kg
14.96 kg 14.94 kg 15.00 kg 14.98 kg

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.

13 solutions

Discussions for this problem are now closed

Thaddeus Abiy
Mar 1, 2014

This problem is a simpler version of the Knapsack problem . It can be solved recursively.Let C a p ( x ) Cap(x) be the maximum amount of water that can be carried for a weight limit of x x . And let v 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 } ) Cap(x)=max( \left\{ Cap(x-{v}_{0})+{v}_{0} , Cap(x-{v}_{1})+{v}_{1},Cap(x-{v}_{2})+{v}_{2} ...,Cap(x-{v}_{i})+{v}_{i} \right\} )

We are basically subtracting each value of the set of bottle( v ) v) values from the limit( 15 k g 15kg ) and seeing which difference yields the maximum value..

This can be implemented easily in python..

Save = {} #Always save values when using recursive functions
          #it saves a lot of time and avoids recomputation
def Cap(Capacity,values):
    if Capacity in Save:
        return Save[Capacity]
    vs = []
    if Capacity in values:
        return Capacity
    if Capacity <= 0:
        return 0
    for value in values:
        if value > Capacity:
            vs.append(0)
        else:
            vs.append(Cap(Capacity-value,values)+value)
    Save[Capacity] = max(vs)
    return max(vs)


print Cap(1500,[77,110,340,700])/100.0

Note:I multiplied each bottle value by 100 100 ,since it is easier to deal with integers.

include<iostream.h>

include<conio.h>

main () { int arr[5],x,max=-32768,mini=32767; clrscr (); for(x=1;x<=5;x++) { cout<<"Enter the Value="; cin>>arr[x]; if(arr[x]>max) max=arr[x]; if(arr[x]<mini) mini=arr[x]; } cout<<"Maximum number is="<<max<<endl; cout<<"Minimum number is="<<mini<<endl; getch (); return 0; }

Ahad Anjum - 7 years, 3 months ago
Girish Chukka
Feb 27, 2014

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...

include <stdio.h>

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);

}

Carlos E. C. do Nascimento - 7 years, 3 months ago

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)

Anurag Poddar - 7 years, 3 months ago

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

Prashant Sharma - 7 years, 3 months ago

this will give you the right answer, but the ranges should be +1, since this is computer science!

Jackie Bow - 7 years, 3 months ago

plz can u tell me what kind of standard this question is

Haji Khalil - 7 years, 3 months ago
Ananay Garg
May 27, 2014

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.

Debajyoti Ghosh
Apr 19, 2014

/ C++ code /

include<stdio.h>

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); }

up as a code block

Amish Naidu
Mar 15, 2014

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;
}
Dinesh Inspire
Mar 15, 2014

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)
Apoorva Wathodkar
Mar 12, 2014

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)

Aditya Mishra
Mar 1, 2014

c++ code for mingw.not efficient but I love recursive brute force.

btw divide solution by 100 ;-)

include <iostream>

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;

}

Lokesh Sharma
Mar 1, 2014

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
Ahmed Abdelbasit
Feb 28, 2014

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

Aakarshit Uppal
Feb 27, 2014

Enter all the 4 options or Start entering from 15.00 and go backwards (this is small version for MCQ - type)

 #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

testphp net - 7 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...