Advertisement
mdamyanova

SoftUni Water Supplies

Jul 3rd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02.SoftUniWaterSupplies
  4. {
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     public class SoftUniWaterSupplies
  9.     {
  10.         public static void Main()
  11.         {
  12.             var totalAmountOfWater = int.Parse(Console.ReadLine());
  13.             decimal[] itemsInTheArray = Console.ReadLine().Split(' ').Select(decimal.Parse).ToArray();
  14.             int bottleCapacity = int.Parse(Console.ReadLine());
  15.             var bottlesLeft = 0;
  16.             var indexesOfLeftBottles = new List<int>();
  17.  
  18.             decimal litersFilled = 0;
  19.             if (totalAmountOfWater % 2 == 0)
  20.             {
  21.                 for (int i = 0; i < itemsInTheArray.Length; i++)
  22.                 {
  23.                     litersFilled += bottleCapacity - itemsInTheArray[i];
  24.                     if (litersFilled > totalAmountOfWater)
  25.                     {
  26.                         bottlesLeft++;
  27.                         indexesOfLeftBottles.Add(i);
  28.                     }
  29.                 }
  30.             }
  31.             else
  32.             {
  33.                 for (int i = itemsInTheArray.Length - 1; i >= 0; i--)
  34.                 {
  35.                     litersFilled += bottleCapacity - itemsInTheArray[i];
  36.                     if (litersFilled > totalAmountOfWater)
  37.                     {
  38.                         bottlesLeft++;
  39.                         indexesOfLeftBottles.Add(i);
  40.                     }
  41.                 }
  42.             }
  43.             if (litersFilled > totalAmountOfWater)
  44.             {
  45.                 Console.WriteLine("We need more water!");
  46.                 Console.WriteLine($"Bottles left: {bottlesLeft}");
  47.                 Console.WriteLine($"With indexes: {string.Join(", ", indexesOfLeftBottles)}");
  48.                 Console.WriteLine($"We need {litersFilled - totalAmountOfWater} more liters!");
  49.             }
  50.             else
  51.             {
  52.                 Console.WriteLine("Enough water!");
  53.                 Console.WriteLine($"Water left: {totalAmountOfWater - litersFilled}l.");
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement