Breaking

Wednesday 7 June 2017

Filling Cans

                         Filling Cans

                                Image result for c++
Two cans are with capacity X and Y liters.
The program must determine the number of steps required to obtain exactly Z litres of liquid in one of the cans.At the beginning both cans are empty.
 The following operations are counted as "steps".
- emptying a vessel,
- filling a vessel,
- pouring liquid from one can to the other, without spilling, until one of the cans is either full or empty.

If it is not possible to obtain Z liters exactly then the output must be -1.



Boundary Conditions:
0 < X <= 100
0 < Y <= 100
0 < Z <= 100

Input Format:

First line will contain the value of X
Second line will contain the value of Y
Third line will contain the value of Z

Output Format:

The number of steps required as an integer.
 If it is not possible to obtain Z liters then the output is -1.

Sample Input/Output:

Example 1:

Input:
5
2
3

Output:
2

Explanation:
Here X=5, Y=2
Step 1: Pour 5 liters of liquid into 5 liter can
Step 2: Pour 2 liters from 5 liter can into 2 liter can.
Now the 5 liter can will have 3 liters which is Z.
 Hence 2 steps are required.

Example 2:
Input:
2
3
4

Output:-
1

Explanation:
Z is greater than X and Y. Hence it is not possible to have 4 liters in any one of the cans. Hence output is -1.


Code:


  1. #include <iostream>
  2.   
  3. using namespace std;
  4.  
  5. int main(int argc, char** argv)
  6. {
  7.     int x,y,z,a,s,count=0;
  8.     cin>>x>>y>>z;
  9.     s=y;
  10.     t:if(x>y && x>z){
  11.         a=x-s;
  12.         count+=2;
  13.         if(a==z){
  14.             cout<<count;
  15.         }
  16.         else if(x>a && a>z){
  17.             s=s+y;
  18.             goto t;
  19.         }
  20.         else{
  21.             cout<<"-1";
  22.         }
  23.     }
  24.     else{
  25.         cout<<"-1";
  26.     }

No comments:

Post a Comment

Like