Saturday 5 September 2015

Question 8

Problem : Milk Man and His Bottles A Milkman serves milk in packaged bottles of varied sizes. The possible size of the bottles are {1, 5, 7 and 10} litres. He wants to supply desired quantity using as less bottles as possible irrespective of the size. Your objective is to help him find the minimum number of bottles required to supply the given demand of milk.
  Input Format: First line contains number of test cases N Next N lines, each contain a positive integer Liwhich corresponds to the demand of milk. 
  Output Format: For each input Li, print the minimum number of bottles required to fulfill the demand 
 Constraints: 1 <= N <= 1000 Li > 0 1 <= i <= N Sample Input and Output
SNo.
Input
Output
12 17 652 7
  Explanation: Number of test cases is 2
  1. In first test case, demand of milk is 17 litres which can be supplied using minimum of 2 bottles as follows
    • 1 x 10 litres and
    • 1 x 7 litres
  2. In second test case, demand of milk is 65 litres which can be supplied using minimum of 7 bottles as follows
    • 6 x 10 litres and
    • 1 x 5 litres

 here is the code in  c

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,nb,q,n;
clrscr();
printf("no of tests : ");
scanf("%d",&n);//no of test cases
for(i=0;i<n;i++)
{
printf("quantity of milk =");
scanf("%d",&q);
nb=0;
r=q%10;
if(q>=10)
{
if(r==2||r==9||r==3||r==4)
{
j=q/10;
nb=nb+j-1;
q=q%10+10;
}
else
{
j=q/10;
nb=nb+j;
q=q-(j*10);
}
}
if(q>=7)
{
j=q/7;
nb=nb+j;
q=q%7;
}
if(q>=5)
{
j=q/5;
nb=nb+j;
q=q%5;
}
nb=nb+q;
printf("minimum no of bottles = %d\n",nb);
}
getch();
}


hope this helps

3 comments:

  1. how to do you find that 2 9 3 4 can you explain me?

    ReplyDelete
  2. Its because they're special cases,
    2 - take 12 -- normally you'll get (10L)*1 + (1L)*2 = 3bottles
    but the no. can be reduced even further using, (7L)*1 +(5L)*1 = 2 bottles.
    similarly for others:
    13 : 7L,5L,1L instead of 10L,1L,1L,1L
    14 : 7L,7L instead of 10L,1L,1L,1L,1L
    19 : 7L,7L,5L instead of 10L,(1L)*9
    So if you consider these cases, then these occur when n % 10 == 2 or 3 or 4 or 9.
    Hope this Helps !

    ReplyDelete
  3. Brother Can You Explain 2 3 4 9 In Detail. Will Help A Lot

    ReplyDelete