Advertisement
elena1234

Cups and Bottles-C# Advanced

Dec 15th, 2020 (edited)
467
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 1 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CupsAndBottles
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] cupsCapacity = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.             var queueForCupsCapacity = new Queue<int>(cupsCapacity);
  13.             int[] bottleWithWatter = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.             var stackForBottleWithWater = new Stack<int>(bottleWithWatter);
  15.             int wastedWater = 0;
  16.             while (queueForCupsCapacity.Count > 0 && stackForBottleWithWater.Count > 0)
  17.             {
  18.                 int currentBottel = stackForBottleWithWater.Peek();
  19.                 int currentCupValue = queueForCupsCapacity.Peek();
  20.                 if (currentCupValue > currentBottel) // the currentCup is bigger than the currentBottel
  21.                 {
  22.                     int reducedCupValue = currentCupValue - currentBottel; // filling is done and there is left capacity in the cup
  23.                     stackForBottleWithWater.Pop();
  24.                     while (reducedCupValue > 0 && stackForBottleWithWater.Count > 0) // until reducedCupValue <= 0 or there aren't any bottels
  25.                     {
  26.                         int nextBottel = stackForBottleWithWater.Peek(); // let's filling the cup with the nextBottel
  27.                         if (nextBottel > reducedCupValue)
  28.                         {
  29.                             wastedWater = wastedWater + (nextBottel - reducedCupValue); //there are wastedWater
  30.                             reducedCupValue -= nextBottel;                          
  31.                         }
  32.  
  33.                         else
  34.                         {
  35.                             reducedCupValue -= nextBottel; // reducedCupValue <= 0 and there aren't wastedWater
  36.                         }
  37.  
  38.                         stackForBottleWithWater.Pop(); // every time remove the currentBottel
  39.                     }
  40.  
  41.                     queueForCupsCapacity.Dequeue(); // in the end remove the currentCup, because reducedCupValue <= 0
  42.                 }
  43.  
  44.                 else if (currentBottel >= currentCupValue) // the currentBottel is bigger than the currentCup
  45.                 {
  46.                     wastedWater = wastedWater + (currentBottel - currentCupValue);
  47.                     stackForBottleWithWater.Pop();
  48.                     queueForCupsCapacity.Dequeue();
  49.                 }
  50.             }
  51.  
  52.             if (stackForBottleWithWater.Count > 0)
  53.             {
  54.                 Console.WriteLine($"Bottles: {string.Join(" ",stackForBottleWithWater)}");
  55.                 Console.WriteLine($"Wasted litters of water: {wastedWater}");
  56.             }
  57.  
  58.             else if (queueForCupsCapacity.Count > 0)
  59.             {
  60.                 Console.WriteLine($"Cups: {string.Join(" ", queueForCupsCapacity)}");
  61.                 Console.WriteLine($"Wasted litters of water: {wastedWater}");
  62.             }          
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement