Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _02.Entertrain
- {
- class Program
- {
- static void Main(string[] args)
- {
- int locomotivePower = int.Parse(Console.ReadLine());
- List<int> trainList = new List<int>();
- string input = string.Empty;
- while ((input = Console.ReadLine()) != "All ofboard!")
- {
- int wagon = int.Parse(input);
- trainList.Add(wagon);
- int sum = trainList.Sum();
- if (sum > locomotivePower)
- {
- decimal average = sum / trainList.Count;
- var nearest = trainList
- .OrderBy(x => Math.Abs((long)x - average))
- .First();
- // по този начин вадим най-близкото до желаното число
- // в този случай average e нашата цел
- trainList.Remove(nearest); // ще махне първото срещнато // trainList.RemoveAll(nearest)
- }
- }
- trainList.Reverse();
- Console.WriteLine(string.Join(" " , trainList) + $" {locomotivePower}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement