Advertisement
Guest User

Untitled

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