The last C problem I need to do, and for me it's quite difficult and I'd like to learn how to do it. Thanks so much
Question 3: Juggl(ug)ing
A cookery set contains several jugs of different sizes, each of which has a known capacity in oz (fluid ounces).
There are no graduations on the jugs, so the only way to measure the liquid is by filling up a jug
completely, pouring as much liquid as possible from one jug into another or by emptying a jug. A step
consists of performing one of these three operations.
Initially all the jugs are empty. To measure n oz it is necessary to perform a sequence of steps and finish
with exactly n oz in one of the jugs.
For example, suppose we have two jugs: jug A holds 3 oz and jug B holds 5 oz.
• If we fill up jug B and then pour as much as possible from jug B into jug A, we would have (after 2
steps) 3 oz in jug A and 2 oz in jug B. This is one way to measure 2 oz.
• If we now empty jug A, pour the 2 oz from jug B into jug A, fill jug B and finally pour as much as
possible from jug B into jug A, we would finish (after 6 steps) with 3 oz in jug A and 4 oz in jug B.
We have now measured 4 oz.
3(a) [ 23 marks ]
Write a program that, given the capacities of several jugs, determines the shortest
number of steps necessary to measure n oz.
The input will consist of two lines. The first line will contain two integers j (1 " j " 3)
then n (1 " n " 250), indicating the number of jugs and the required amount to
measure respectively. The second line will contain j integers, each between 1 and
250 inclusive, indicating the capacity of the jugs.
You should output a single integer giving the minimum number of steps necessary to
measure n oz.
Your program will only be asked to measure amounts that are possible.