Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace associativeArrays
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class StartUp
- {
- public static void Main()
- {
- string until = string.Empty;
- var dict = new Dictionary<string, List<string>>();
- while ((until = Console.ReadLine()) != "end")
- {
- string[] tokens = until.Split(new[] { " : " }, StringSplitOptions.RemoveEmptyEntries);
- string nameOfCourse = tokens[0];
- string student = tokens[1];
- if (dict.ContainsKey(nameOfCourse) == false)
- {
- dict.Add(nameOfCourse, new List<string>());
- dict[nameOfCourse].Add(student);
- }
- // Проверка дали един и същ курсист е записан в повече от един път в курса.
- else if (dict[nameOfCourse].Contains(student) == false)
- {
- dict[nameOfCourse].Add(student);
- }
- }
- // Подредба по бройка на студенти в низходящ ред както е по условие.
- var ordered = dict.OrderByDescending(x => x.Value.Count);
- foreach (var kvp in ordered)
- {
- string program = kvp.Key;
- // Подредба по азбучен ред.
- var students = kvp.Value.OrderBy(x => x).ToList();
- Console.WriteLine($"{program}: {kvp.Value.Count}");
- //Console.Write($"-- ");
- //Console.WriteLine(string.Join($"{Environment.NewLine}-- ", students));
- // Това е по-чисто принтиране, но и горното работи.
- students.ForEach(x => { Console.WriteLine($"-- {x}"); });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment