fbinnzhivko

02. SoftUni Water Supplies View code

Sep 15th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace WaterSupply
  5. {
  6.     class WaterSupply
  7.     {
  8.         static void Main()
  9.         {
  10.             double litersOfWater = long.Parse(Console.ReadLine());
  11.             double[] arr = Console.ReadLine().Split().Select(double.Parse).ToArray();
  12.             uint bottleCapacity = uint.Parse(Console.ReadLine());
  13.  
  14.             double litersToFill = arr.Length * bottleCapacity - arr.Sum();
  15.  
  16.             if (litersOfWater >= litersToFill)
  17.             {
  18.                 Console.WriteLine("Enough water!");
  19.                 Console.WriteLine("Water left: {0}l.", litersOfWater - litersToFill);
  20.             }
  21.             else
  22.             {
  23.                 List<long> indexesOfLeft = new List<long>();
  24.  
  25.                 if (litersOfWater % 2 == 0)
  26.                 {
  27.                     for (int i = 0; i < arr.Length; i++)
  28.                     {
  29.                         if (arr[i] < bottleCapacity)
  30.                         {
  31.                             double diff = bottleCapacity - arr[i];
  32.  
  33.                             if (litersOfWater >= diff)
  34.                             {
  35.                                 litersOfWater = litersOfWater - diff;
  36.                                 arr[i] += diff;
  37.                             }
  38.                             else
  39.                             {
  40.                                 indexesOfLeft.Add(i);
  41.                             }
  42.                         }
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     for (int i = arr.Length - 1; i >= 0; i--)
  48.                     {
  49.                         if (arr[i] < bottleCapacity)
  50.                         {
  51.                             double diff = bottleCapacity - arr[i];
  52.  
  53.                             if (litersOfWater >= diff)
  54.                             {
  55.                                 litersOfWater = litersOfWater - diff;
  56.                                 arr[i] += diff;
  57.                             }
  58.                             else
  59.                             {
  60.                                 indexesOfLeft.Add(i);
  61.                             }
  62.                         }
  63.                     }
  64.                 }
  65.                 Console.WriteLine("We need more water!");
  66.                 Console.WriteLine("Bottles left: {0}", indexesOfLeft.Count);
  67.                 Console.WriteLine("With indexes: {0}", string.Join(", ", indexesOfLeft));
  68.                 Console.WriteLine("We need {0} more liters!", arr.Length * bottleCapacity - arr.Sum() - litersOfWater);
  69.             }
  70.         }
  71.     }
  72. }
Add Comment
Please, Sign In to add comment