Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P08_Ranking
  6. {
  7.     class Ranking
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var validationData = new Dictionary<string, string>();
  12.             var studentsInfo = new SortedDictionary<string, Dictionary<string, int>>();
  13.  
  14.             var input = Console.ReadLine();
  15.  
  16.             while (input != "end of contests")
  17.             {
  18.                 var inputArgs = input.Split(":", StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 var contest = inputArgs[0];
  21.                 var password = inputArgs[1];
  22.  
  23.                 if (!validationData.ContainsKey(contest))
  24.                 {
  25.                     validationData.Add(contest, password);
  26.                 }
  27.  
  28.                 input = Console.ReadLine();
  29.             }
  30.  
  31.             input = Console.ReadLine();
  32.  
  33.             while (input != "end of submissions")
  34.             {
  35.                 var inputArgs = input.Split("=>");
  36.  
  37.                 var contest = inputArgs[0];
  38.                 var password = inputArgs[1];
  39.                 var username = inputArgs[2];
  40.                 var points = int.Parse(inputArgs[3]);
  41.                 var isContestValid = validationData.ContainsKey(contest) && validationData.ContainsValue(password);
  42.  
  43.                 AddStudentsContestsWithPoints(isContestValid, studentsInfo, username, contest, points);
  44.  
  45.                 input = Console.ReadLine();
  46.             }
  47.  
  48.             var maxResult = 0;
  49.             var bestcandidate = String.Empty;
  50.  
  51.             maxResult = FindBestCandidateWithMaxResult(studentsInfo, maxResult, ref bestcandidate);
  52.  
  53.             Console.WriteLine($"Best candidate is {bestcandidate} with total {maxResult} points.");
  54.             Console.WriteLine("Ranking:");
  55.  
  56.             PrintAllStudentsWithResults(studentsInfo);
  57.         }
  58.  
  59.         private static void AddStudentsContestsWithPoints(bool isContestValid, SortedDictionary<string, Dictionary<string, int>> studentsInfo, string username,
  60.             string contest, int points)
  61.         {
  62.             if (isContestValid)
  63.             {
  64.                 if (!studentsInfo.ContainsKey(username))
  65.                 {
  66.                     studentsInfo.Add(username, new Dictionary<string, int>());
  67.                 }
  68.  
  69.                 if (!studentsInfo[username].ContainsKey(contest))
  70.                 {
  71.                     studentsInfo[username].Add(contest, points);
  72.                 }
  73.                 else if (studentsInfo[username][contest] < points)
  74.                 {
  75.                     studentsInfo[username][contest] = points;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         private static int FindBestCandidateWithMaxResult(SortedDictionary<string, Dictionary<string, int>> studentsInfo, int maxResult,
  81.             ref string bestcandidate)
  82.         {
  83.             foreach (var (name, contests) in studentsInfo)
  84.             {
  85.                 var sumResult = 0;
  86.  
  87.                 foreach (var (contest, points) in contests)
  88.                 {
  89.                     sumResult += points;
  90.                 }
  91.  
  92.                 if (sumResult > maxResult)
  93.                 {
  94.                     maxResult = sumResult;
  95.                     bestcandidate = name;
  96.                 }
  97.             }
  98.  
  99.             return maxResult;
  100.         }
  101.  
  102.         private static void PrintAllStudentsWithResults(SortedDictionary<string, Dictionary<string, int>> studentsInfo)
  103.         {
  104.             foreach (var (username, contests) in studentsInfo)
  105.             {
  106.                 Console.WriteLine($"{username}");
  107.  
  108.                 foreach (var (contest, points) in contests.OrderByDescending(x => x.Value))
  109.                 {
  110.                     Console.WriteLine($"#  {contest} -> {points}");
  111.                 }
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement