IPetrov007

MeanMedianMode

Mar 27th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. class Solution
  7. {
  8. static void Main(String[] args)
  9. {
  10. var n = int.Parse(Console.ReadLine());
  11. var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  12.  
  13. var mean = input.Sum() / (double)n;
  14. input = input.OrderBy(x => x).ToArray();
  15. var median = (input[n / 2] + input[(n / 2 - 1)]) / 2.0;
  16.  
  17. var repeatInt = new Dictionary<int, int>();
  18. foreach (var num in input)
  19. {
  20. if (!repeatInt.ContainsKey(num))
  21. {
  22. repeatInt.Add(num, 0);
  23. }
  24. repeatInt[num]++;
  25. }
  26.  
  27. repeatInt = repeatInt.OrderBy(x => x.Value).ThenBy(x => x.Key).ToDictionary(x =>x.Key, x => x.Value);
  28.  
  29. var mode = 0;
  30. if (repeatInt.Values.Max() != 1)
  31. {
  32. mode = repeatInt.Values.Max();
  33. }
  34. else
  35. {
  36. mode = repeatInt.Keys.Min();
  37. }
  38.  
  39. Console.WriteLine($"{mean:F1}");
  40. Console.WriteLine($"{median:F1}");
  41. Console.WriteLine($"{mode}");
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment