Guest User

Untitled

a guest
Dec 31st, 2017
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.Entertrain
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int locomotivePower = int.Parse(Console.ReadLine());
  14.  
  15. List<int> trainList = new List<int>();
  16. string input = string.Empty;
  17.  
  18. while ((input = Console.ReadLine()) != "All ofboard!")
  19. {
  20. int wagon = int.Parse(input);
  21. trainList.Add(wagon);
  22.  
  23. int sum = trainList.Sum();
  24.  
  25. if (sum > locomotivePower)
  26. {
  27. decimal average = sum / trainList.Count;
  28.  
  29. var nearest = trainList
  30. .OrderBy(x => Math.Abs((long)x - average))
  31. .First();
  32.  
  33. // по този начин вадим най-близкото до желаното число
  34. // в този случай average e нашата цел
  35.  
  36. trainList.Remove(nearest); // ще махне първото срещнато // trainList.RemoveAll(nearest)
  37. }
  38. }
  39.  
  40. trainList.Reverse();
  41.  
  42. Console.WriteLine(string.Join(" " , trainList) + $" {locomotivePower}");
  43. }
  44. }
  45. }
RAW Paste Data