Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Design;
- using System.Linq;
- namespace Ranking_Sets_and_Dictionaries
- {
- class Program
- {
- static void Main(string[] args)
- {
- Dictionary<string, string> contestInfo = new Dictionary<string, string>();
- string input = Console.ReadLine();
- while (input != "end of contests")
- {
- string[] data = input.Split(":");
- string contest = data[0];
- string passContest = data[1];
- contestInfo.Add(contest, passContest);
- input = Console.ReadLine();
- }
- Dictionary<string, Dictionary<string, int>> usersInfo = new Dictionary<string, Dictionary<string, int>>();
- string info = Console.ReadLine();
- while (info != "end of submissions")
- {
- string[] data = info.Split("=>");
- string contest = data[0];
- string password = data[1];
- string username = data[2];
- int points = int.Parse(data[3]);
- if (contestInfo.ContainsKey(contest))
- {
- if (contestInfo[contest] == password)
- {
- if (!usersInfo.ContainsKey(username))
- {
- usersInfo.Add(username, new Dictionary<string, int>());
- }
- if (!usersInfo[username].ContainsKey(contest))
- {
- usersInfo[username].Add(contest, points);
- }
- else
- {
- if (usersInfo[username][contest] < points)
- {
- usersInfo[username][contest] = points;
- }
- }
- }
- }
- info = Console.ReadLine();
- }
- KeyValuePair<string, int> bestCandidate = BestCandidate(usersInfo);
- Console.WriteLine($"Best candidate is {bestCandidate.Key} with total {bestCandidate.Value} points.");
- Console.WriteLine("Ranking:");
- foreach (var student in usersInfo.OrderBy(s=>s.Key))
- {
- Console.WriteLine(student.Key);
- foreach (var contest in student.Value.OrderByDescending(p=>p.Value))
- {
- Console.WriteLine($"# {contest.Key} -> {contest.Value}");
- }
- }
- }
- private static KeyValuePair<string, int> BestCandidate(Dictionary<string, Dictionary<string, int>> usersInfo)
- {
- int bestPoints = 0;
- string bestCand = string.Empty;
- foreach (var (key, value) in usersInfo)
- {
- string username = key;
- int sumPoints = value.Values.Sum();
- if (bestPoints < sumPoints)
- {
- bestPoints = sumPoints;
- bestCand = username;
- }
- }
- KeyValuePair<string, int> bestCandidate = new KeyValuePair<string, int>(bestCand, bestPoints);
- return bestCandidate;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement