Advertisement
DeeAG

01.Ranking

May 27th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _01.Ranking
  6. {
  7.     class Program
  8.     {
  9.         private class Student
  10.         {
  11.             public string Name { get; }
  12.  
  13.             public Dictionary<string, int> ContestWithPoints { get; }
  14.  
  15.             public Student(string name)
  16.             {
  17.                 Name = name;
  18.                 ContestWithPoints = new Dictionary<string, int>();
  19.             }
  20.         }
  21.         static void Main(string[] args)
  22.         {
  23.             Dictionary<string, string> contests = new Dictionary<string, string>();
  24.             Dictionary<string, Student> userPoints = new Dictionary<string, Student>();
  25.  
  26.             string line;
  27.             while ((line = Console.ReadLine()) != "end of contests")
  28.             {
  29.                 string[] parts = line.Split(":", StringSplitOptions.RemoveEmptyEntries);
  30.  
  31.                 string contest = parts[0];
  32.                 string password = parts[1];
  33.  
  34.                 if (!contests.ContainsKey(contest))
  35.                 {
  36.                     contests.Add(contest, password);
  37.                 }
  38.             }
  39.  
  40.             while ((line = Console.ReadLine()) != "end of submissions")
  41.             {
  42.                 string[] parts = line.Split("=>", StringSplitOptions.RemoveEmptyEntries);
  43.                 string contest = parts[0];
  44.                 string password = parts[1];
  45.                 string username = parts[2];
  46.                 int points = int.Parse(parts[3]);
  47.  
  48.                 if (!contests.ContainsKey(contest) || contests[contest] != password)
  49.                 {
  50.                     continue;
  51.                 }
  52.  
  53.                 if (!userPoints.ContainsKey(username))
  54.                 {
  55.                     userPoints.Add(username, new Student(username));
  56.                 }
  57.  
  58.                 Student student = userPoints[username];
  59.  
  60.                 if (!student.ContestWithPoints.ContainsKey(contest))
  61.                 {
  62.                     student.ContestWithPoints.Add(contest, points);
  63.                 }
  64.  
  65.                 if (student.ContestWithPoints[contest] < points)
  66.                 {
  67.                     student.ContestWithPoints[contest] = points;
  68.                 }
  69.             }
  70.  
  71.             PrintTheRanking(userPoints.Values.ToList());
  72.         }
  73.         private static void PrintTheRanking(List<Student> listWithStudents)
  74.         {
  75.             var bestCandidate = listWithStudents
  76.                 .OrderByDescending(x => x.ContestWithPoints.Values.Sum())
  77.                 .First();
  78.  
  79.             Console.WriteLine($"Best candidate is {bestCandidate.Name} with total {bestCandidate.ContestWithPoints.Values.Sum()} points.");
  80.            
  81.             Console.WriteLine("Ranking:");
  82.            
  83.             foreach (var student in listWithStudents.OrderBy(x => x.Name))
  84.             {
  85.                 Console.WriteLine(student.Name);
  86.  
  87.                 foreach (var (contest, points) in student.ContestWithPoints.OrderByDescending(x => x.Value))
  88.                 {
  89.                     Console.WriteLine($"#  {contest} -> {points}");
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement