Advertisement
braveheart1989

SoftUniWaterSupplies

Jun 10th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _02.SoftUniWaterSupplies
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             decimal water = decimal.Parse(Console.ReadLine());
  14.             decimal[] bottles = Console.ReadLine().Split().Select(decimal.Parse).ToArray();
  15.             decimal capacity = decimal.Parse(Console.ReadLine());
  16.             decimal neededWater = 0;
  17.             long index = -1;
  18.  
  19.             if (water % 2 == 0)
  20.             {
  21.                 for (long i = 0; i < bottles.Length; i++)
  22.                 {
  23.                     neededWater = capacity - bottles[i];
  24.                     if (water >= neededWater)
  25.                     {
  26.                         bottles[i] += neededWater;
  27.                         water -= neededWater;
  28.                     }
  29.                     else
  30.                     {
  31.                         index = i;
  32.                         bottles[i] += water;
  33.                         break;
  34.                     }
  35.                 }
  36.                 if (index == -1)
  37.                 {
  38.                     Console.WriteLine("Enough water!");
  39.                     Console.WriteLine("Water left: {0}l.", water);
  40.                 }
  41.                 else
  42.                 {
  43.                     long bottlesCount = bottles.Where(bottle => bottle < capacity).Count();
  44.                     List<long> indexex = new List<long>();
  45.                     decimal waterShortage = 0;
  46.                     for (long i = index; i < bottles.Length; i++)
  47.                     {
  48.                         indexex.Add(i);
  49.                         waterShortage += capacity - bottles[i];
  50.                     }
  51.                     Console.WriteLine("We need more water!");
  52.                     Console.WriteLine("Bottles left: {0}", bottlesCount);
  53.                     Console.WriteLine("With indexes: {0}", string.Join(", ", indexex));
  54.                     Console.WriteLine("We need {0} more liters!", waterShortage);
  55.  
  56.                 }
  57.             }
  58.             else
  59.             {
  60.                 for (long i = bottles.Length - 1; i >= 0; i--)
  61.                 {
  62.                     neededWater = capacity - bottles[i];
  63.                     if (water >= neededWater)
  64.                     {
  65.                         bottles[i] += neededWater;
  66.                         water -= neededWater;
  67.                     }
  68.                     else
  69.                     {
  70.                         index = i;
  71.                         bottles[i] += water;
  72.                         break;
  73.                     }
  74.                 }
  75.                 if (index == -1)
  76.                 {
  77.                     Console.WriteLine("Enough water!");
  78.                     Console.WriteLine("Water left: {0}l.", water);
  79.                 }
  80.                 else
  81.                 {
  82.                     long bottlesCount = bottles.Where(bottle => bottle < capacity).Count();
  83.                     List<long> indexex = new List<long>();
  84.                     decimal waterShortage = 0;
  85.                     for (int i = bottles.Length - 1; i >= 0; i--)
  86.                     {
  87.                         waterShortage += capacity - bottles[i];
  88.                         if (waterShortage > 0)
  89.                         {
  90.                             indexex.Add(i);
  91.                         }
  92.                     }                  
  93.                     Console.WriteLine("We need more water!");
  94.                     Console.WriteLine("Bottles left: {0}", bottlesCount);
  95.                     Console.WriteLine("With indexes: {0}", string.Join(", ", indexex));
  96.                     Console.WriteLine("We need {0} more liters!", waterShortage);
  97.  
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement