Best answer of tcs codevita 2015 questions

Friends, you may have faced C++ questions in Codevita exam organized by TCS. Here is an answer to a question asked in TCS codevita. If you need more questions go to TCS codevita important questions 2015.

answer of tcs codevita 2015 questions

TCS codevita Question

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

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

TCS codevita Answer

Solution : Milk Man and His Bottles

#include <iostream>
using namespace std;
int minimum(int n1,int n2,int n3)
{
int a[3]={n1,n2,n3},minim=a[0];
for(int i=0;i<3;i++)
{
if(minim>a[i])
minim=a[i];
}
return minim;
}
int f(int n,int no)
{
if(n==1||n==5||n==7||n==10)
return no+1;
if(n>1&&n<5)
return (f(n-1,no+1));
if(n>5&&n<7)
return (f(n-5,no+1));
if(n>7&&n<10)
return (f(n-7,no+1));
if(n>10)
{
return(minimum(f(n-7,no+1),f(n-10,no+1),f(n-5,no+1)));
}
cout<<“error”;
return 0;
}
int main()
{int n,cases;
cout<<“Enter cases: “;
cin>>cases;
for(int i=0; i<cases; i++){
cout<<“Enter demand: “;
cin>>n;
cout<<“minimum no of bottles required are: “;
cout<<f(n,0)<<“n”;
}
return 0;
}

Mr. Sai kalyan gave me the code for this milkman question in java. Here it is

/* package codechef; // don’t place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Milkman
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader user = new BufferedReader(new InputStreamReader(System.in));
int test_case = Integer.parseInt(user.readLine());
int[] bottles = {10,7,5,1};
int[] result = new int[test_case];
int milk,lts,btl,j;
for(int i=0; i<test_case;i++)
{
milk = Integer.parseInt(user.readLine());
j=0;
btl=0;
while(milk>0)
{
lts   = milk/bottles[j];
milk -= bottles[j]*lts;
j++;
btl += lts;
}
result[i] = btl;
}
for(int i=0;i<test_case;i++)
System.out.println(result[i]);
}
}

The Codevita problem code is well understood if you have any problem in any of the codes give your suggestion in the comment section. I will be Happy to Help you.

Help others by Sharing this post. Thank you.


Related Articles

Leave a Comment