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
17
65
Sample Output
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
C++ Implementation:
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
17
65
Sample Output
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
C++ Implementation:
#include <iostream> using namespace std; int main() { int N; cin>>N; while(N--) { int L; int bottles = 0; int supply = 0; cin>>L; if(L>11 && (L%10==2 || L%10==3 || L%10==4 || L%10==9)) { if(L%10==2) bottles = (L-12)/10 + 2; else if(L%10==3) bottles = (L-13)/10 + 3; else if(L%10==4) bottles = (L-14)/10 + 2; else bottles = (L-19)/10 + 3; } else { while(supply+10 <= L) { supply += 10; bottles++; } while(supply+7 <= L) { supply += 7; bottles++; } while(supply+5 <= L) { supply += 5; bottles++; } while(supply+1 <= L) { supply += 1; bottles++; } } if(N!=0) cout<<bottles<<endl; else cout<<bottles; } return 0; }
Other Problems from TCS Mockvita - 1 and Mockvita - 2:
Solution : MockVita-I Problem D
Solution : MockVita-II Problem C
Solution : MockVita-II Problem B
Solution : MockVita-II Problem D
Solution : MockVita-II Problem A
Solution : MockVita-I Problem B
Solution : MockVita-I Problem C
Solution : MockVita-I Problem F
Solution : MockVita-I Problem A