Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2016
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 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.  
  8. class Program
  9. {
  10.     static void Main(string[] args)
  11.     {
  12.         System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
  13.         customCulture.NumberFormat.NumberDecimalSeparator = ".";
  14.         System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
  15.  
  16.         double supply = double.Parse(Console.ReadLine());
  17.         double a = supply;
  18.         double[] bottles = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
  19.         double capacity = double.Parse(Console.ReadLine());
  20.         double numberOfBottles = 0;
  21.         bool filledBottles = false;
  22.         List<long> indexes = new List<long>();
  23.         double littersNeeded = 0;
  24.         if (supply % 2 != 0)
  25.         {
  26.             for (int i = bottles.Length - 1; i >= 0; i--)
  27.             {
  28.                 if (bottles[i] <= capacity && supply > 0)
  29.                 {
  30.                     if (supply < capacity - bottles[i])
  31.                     {
  32.                         bottles[i] += supply;
  33.                         break;
  34.                     }
  35.                     supply -= (capacity - bottles[i]);
  36.                     bottles[i] += (capacity - bottles[i]);
  37.                 }
  38.                 if (bottles[0] == capacity && supply >= 0)
  39.                 {
  40.                     Console.WriteLine("Enough water!");
  41.                     Console.WriteLine("Water left: {0}l.", supply);
  42.                     filledBottles = true;
  43.                 }
  44.             }
  45.             if (!filledBottles)
  46.             {
  47.                 for (int i = bottles.Length - 1; i >= 0; i--)
  48.                 {
  49.                     if (bottles[i] < capacity)
  50.                     {
  51.                         numberOfBottles++;
  52.                         littersNeeded += (capacity - bottles[i]);
  53.                         indexes.Add(i);
  54.                     }
  55.                     if (i == 0)
  56.                     {
  57.                         Console.WriteLine("We need more water!");
  58.                         Console.WriteLine("Bottles left: {0}", numberOfBottles);
  59.                         string joined = string.Join<long>(", ", indexes);
  60.                         Console.WriteLine("With indexes: " + joined);
  61.                         Console.WriteLine("We need {0} more liters!", littersNeeded);
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         if (supply % 2 == 0 && supply == a)
  67.         {
  68.             for (int i = 0; i < bottles.Length; i++)
  69.             {
  70.                 if (bottles[i] <= capacity && supply > 0)
  71.                 {
  72.                     if (supply < capacity - bottles[i])
  73.                     {
  74.                         bottles[i] += supply;
  75.                         break;
  76.                     }
  77.                     supply -= (capacity - bottles[i]);
  78.                     bottles[i] += (capacity - bottles[i]);
  79.                 }                                                         // I added this under && i == bottles.Length - 1
  80.                 if (bottles[bottles.Length - 1] == capacity && supply >= 0 && i == bottles.Length - 1)
  81.                 {
  82.                     Console.WriteLine("Enough water!");
  83.                     Console.WriteLine("Water left: {0}l.", supply);
  84.                     filledBottles = true;
  85.                 }
  86.             }
  87.             if (!filledBottles)
  88.             {
  89.                 for (int i = 0; i < bottles.Length; i++)
  90.                 {
  91.                     if (bottles[i] < capacity)
  92.                     {
  93.                         numberOfBottles++;
  94.                         littersNeeded += (capacity - bottles[i]);
  95.                         indexes.Add(i);
  96.                     }
  97.                     if (i == bottles.Length - 1)
  98.                     {
  99.                         Console.WriteLine("We need more water!");
  100.                         Console.WriteLine("Bottles left: {0}", numberOfBottles);
  101.                         string joined = string.Join<long>(", ", indexes);
  102.                         Console.WriteLine("With indexes: " + joined);
  103.                         Console.WriteLine("We need {0} more liters!", littersNeeded);
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement