Answer on Question #60902 - Programming & Computer Science
Condition: 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.
Solution (In C#):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1._1
{
class Program
{
static void Main(string[] args)
{
int amount = 0, N = 0; // amount of milk, N - the number of bottles
Console.Write("Enter the desired amount of milk: ");
amount = Convert.ToInt32(Console.ReadLine());
if (amount / 10 > 0) N = amount / 10; amount = amount - N * 10;
if (amount / 7 > 0) { N++; amount -= 7; }
if (amount / 5 > 0) { N++; amount -= 5; }
if (amount > 0) N += amount;
Console.WriteLine("Minimum number of bottles: " + N);
Console.ReadLine();
}
}
}Examples of the work program:
Input:14
Output:5
Input:30
Output:3
Input:78
Output:9
http://www.AssignmentExpert.com
Comments