Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- static void Main()
- {
- double amountOfWater = double.Parse(Console.ReadLine());
- double[] bottles = Console.ReadLine().Split().Select(double.Parse).ToArray();
- double bottleCapacity = double.Parse(Console.ReadLine());
- int indexOfLastFullBottle = 0;
- double neededWater = 0;
- if (amountOfWater%2 == 0) //Starts from left to right
- {
- EvenWater(ref amountOfWater, bottles, bottleCapacity, ref indexOfLastFullBottle, ref neededWater);
- }
- else // starts from the end
- OddWater(ref amountOfWater, bottles, bottleCapacity, ref indexOfLastFullBottle, ref neededWater);
- }
- private static void OddWater(ref double amountOfWater, double[] bottles, double bottleCapacity,
- ref int indexOfLastFullBottle, ref double neededWater)
- {
- for (int i = bottles.Length - 1; i >= 0; i--)
- {
- double currentBottle = Math.Abs(bottleCapacity - (int) bottles[i]);
- amountOfWater = amountOfWater - currentBottle;
- if (amountOfWater >= 0)
- {
- indexOfLastFullBottle = i;
- }
- else if (amountOfWater < 0)
- {
- neededWater = -amountOfWater;
- if (i != 0)
- {
- for (int j = indexOfLastFullBottle - 1; j >= 0; j--)
- {
- neededWater += (bottleCapacity - (int) bottles[j]);
- }
- }
- }
- }
- if (amountOfWater >= 0)
- {
- Console.WriteLine("Enough water!");
- Console.WriteLine("Water left: {0}l.", amountOfWater);
- }
- else
- {
- List<int> EmptyBottles = new List<int>();
- for (int i = indexOfLastFullBottle - 1; i >= 0; i--)
- {
- EmptyBottles.Add(i);
- }
- Console.WriteLine("We need more water!");
- Console.WriteLine("Bottles left: {0}", indexOfLastFullBottle);
- Console.WriteLine("With indexes: " + string.Join(", ", EmptyBottles));
- Console.WriteLine("We need {0} more liters!", neededWater);
- }
- }
- private static void EvenWater(ref double amountOfWater, double[] bottles, double bottleCapacity,
- ref int indexOfLastFullBottle, ref double neededWater)
- {
- for (int i = 0; i < bottles.Length; i++)
- {
- double currentBottle = Math.Abs(bottleCapacity - bottles[i]);
- amountOfWater = amountOfWater - currentBottle;
- if (amountOfWater >= 0)
- {
- indexOfLastFullBottle = i;
- neededWater = (bottles.Length - 1 - indexOfLastFullBottle)*bottleCapacity;
- }
- else if (amountOfWater < 0)
- {
- indexOfLastFullBottle = i - 1;
- neededWater = -amountOfWater;
- break;
- }
- }
- if (amountOfWater >= 0)
- {
- Console.WriteLine("Enough water!");
- Console.WriteLine("Water left: {0}l.", amountOfWater);
- }
- else
- {
- List<int> EmptyBottles = new List<int>();
- for (int i = 0; i < indexOfLastFullBottle; i++)
- {
- EmptyBottles.Add(i);
- }
- Console.WriteLine("We need more water!");
- Console.WriteLine("Bottles left: {0}", bottles.Length - 1 - indexOfLastFullBottle);
- Console.WriteLine("We need {0} more liters!", neededWater);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement