angelllo

Judge (More Exercise: Associative Arrays)

Jul 14th, 2020
93
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.Diagnostics.Tracing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace Judge
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var data = new Dictionary<string, Dictionary<string, int>>();
  14. var sumPoints = new Dictionary<string, int>();
  15.  
  16. while (true)
  17. {
  18. string input = Console.ReadLine();
  19. if (input == "no more time")
  20. {
  21. break;
  22. }
  23.  
  24. string[] command = input.Split(" -> ").ToArray();
  25.  
  26. string user = command[0];
  27. string contest = command[1];
  28. int points = int.Parse(command[2]);
  29.  
  30.  
  31. if (!data.ContainsKey(contest))
  32. {
  33. data.Add(contest, new Dictionary<string, int>());
  34. data[contest][user] = points;
  35. }
  36.  
  37. else
  38. {
  39. if (!data[contest].ContainsKey(user))
  40. {
  41. data[contest][user] = points;
  42. }
  43.  
  44. else
  45. {
  46. if (data[contest][user] < points)
  47. {
  48. data[contest][user] = points;
  49. }
  50. }
  51. }
  52.  
  53. if (!sumPoints.ContainsKey(user))
  54. {
  55. sumPoints.Add(user, points);
  56. }
  57. else
  58. {
  59. if (sumPoints[user] < points)
  60. {
  61. sumPoints[user] = points;
  62. }
  63. else
  64. {
  65. sumPoints[user] += points;
  66. }
  67. }
  68. }
  69.  
  70. foreach (var kvp in data)
  71. {
  72. Dictionary<string, int> nested2 = kvp.Value;
  73. Console.WriteLine($"{kvp.Key}: {kvp.Value.Count} participants");
  74. int counter = 1;
  75.  
  76. foreach (var pair in nested2.OrderByDescending(p => p.Value).ThenBy(n => n.Key))
  77. {
  78. Console.WriteLine($"{counter++}. {pair.Key} <::> {pair.Value}");
  79. }
  80. }
  81.  
  82. Console.WriteLine("Individual standings:");
  83. int count = 1;
  84.  
  85. foreach (var mem in sumPoints.OrderByDescending(m => m.Value).ThenBy(c => c.Key))
  86. {
  87. Console.WriteLine($"{count++}. {mem.Key} -> {mem.Value}");
  88. }
  89. }
  90. }
  91. }
RAW Paste Data