YavorGrancharov

Entertrain(examTask02_90%)

Aug 20th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Entertrain
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int locomotivePower = int.Parse(Console.ReadLine());
  12.             string wagonWeight = Console.ReadLine();
  13.  
  14.             List<int> train = new List<int>();
  15.  
  16.             while (wagonWeight != "All ofboard!")
  17.             {
  18.                 train.Add(int.Parse(wagonWeight));
  19.                
  20.                 for (int i = 1; i < train.Count; i++)
  21.                 {
  22.                     int sum = train.Sum();
  23.                     int avg = sum / train.Count();
  24.                     int nearrestWagon = train.OrderBy(x => Math.Abs(x - avg)).First();
  25.  
  26.                     if (sum > locomotivePower)
  27.                     {
  28.                         train.Remove(nearrestWagon);
  29.                     }
  30.                 }
  31.                 wagonWeight = Console.ReadLine();
  32.             }
  33.             Console.WriteLine(string.Join(" ", train.AsEnumerable().Reverse()) + " " + locomotivePower);
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment