Saturday, 15 August 2015

TCS CodeVita Milk Man Problem

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 Li which 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
1

2
17
65

2
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

 Solution in C :

#include<stdio.h>
int main()
{
int c,i,cnt,array[1000],j;

scanf("%d",&c);
if(c>=1&&c<=1000)
{

for(j=0;j<c;j++)
{
    scanf("%d",&i);
    cnt=0;
if(i>0)
{
while(i!=0)
{
    if((i==12||i==13||i==19||i==14))
    {
        cnt--;
        if(i==14)
        {
            cnt=cnt-2;
        }

    }
        if(i>=10)
{
    cnt++;
    i=i-10;
}
else if(i>=7)
{
    cnt++;
    i=i-7;
}
else if(i>=5)
{
    cnt++;
    i=i-5;
}
else if(i>=1)
{
    cnt++;
    i=i-1;
}
}

printf("%d\n",cnt);
}
}
}
return 0;
}

Solution in Java :


import java.util.*;
public class bottle{

private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int n,v,u,m,m1,s,s1,i,v1,w1;
int l[]=new int[9999];
try
{
n=in.nextInt();
if((n>=1)&&(n<=1000))
{
for(i=1;i<=n;i++)
{
if((i>=1)&&(i<=n))
{
l[i]=in.nextInt();
}
}
for(i=1;i<=n;i++)
{
if(l[i]>0)
{

if(l[i]<=9)
{
if((l[i]==1)||(l[i]==5)||(l[i]==7))
{
int a1=1;
System.out.println(a1);
}
if((l[i]==2)||(l[i]==6)||(l[i]==8))
{
int a2=2;
System.out.println(a2);
}
if((l[i]==3)||(l[i]==9))
{
int a3=3;
System.out.println(a3);

}
if(l[i]==4)
{
int a4=4;
System.out.println(a4);
}
}
int z=l[i]/10;
v=z-1;
u=l[i]%10;
if(l[i]>=10)
{
if(u==0)
{
System.out.println(z);
}
if((u==1)||(u==5)||(u==7))
{
v1=z+1;
System.out.println(v1);
}
if((u==2)||(u==4))
{
m=2;
m1=v+m;
System.out.println(m1);
}
if((u==3)||(u==9))
{
s=3;
s1=v+s;
System.out.println(s1);
}
if((u==6)||(u==8))
{
int k1=z+2;
System.out.println(k1);
}
}
}
}
}

}
catch(Exception e)
{

}

}

}


No comments:

Post a Comment