Advertisement
Guest User

ChampionsLeague

a guest
Apr 21st, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. public class Startup
  8. {
  9.     public static void Main()
  10.     {
  11.         Dictionary<string, Dictionary<string, int>> champLeague = new Dictionary<string, Dictionary<string, int>>();
  12.  
  13.         string input = Console.ReadLine();
  14.  
  15.         while (input != "stop")
  16.         {
  17.             string[] commandLine = input.Split('|');
  18.             string team1 = commandLine[0].Trim();
  19.             string team2 = commandLine[1].Trim();
  20.  
  21.             string[] scores1 = commandLine[2].Split(':');
  22.             int team1Goals = int.Parse(scores1[0]);
  23.             int team2Goals = int.Parse(scores1[1]);
  24.  
  25.             string[] scores2 = commandLine[3].Split(':');
  26.             team2Goals += int.Parse(scores2[0]);
  27.             team1Goals += int.Parse(scores2[1]);
  28.  
  29.             FilloutAllTeams(champLeague, team1, team2, team1Goals, team2Goals);
  30.  
  31.             input = Console.ReadLine();
  32.         }
  33.         PrintFinalResult(champLeague);
  34.     }
  35.  
  36.     private static void PrintFinalResult(Dictionary<string, Dictionary<string, int>> champLeague)
  37.     {
  38.  
  39.         //var orderedDictionary = champLeague.OrderByDescending(x => x.Value.Values).ThenBy(x => x.Value.Keys); когато опитам с този код ми гърми с "unhandled exception at least one object must be implemented IComparable"
  40.         foreach (var kvp in champLeague) // трябва тук да е orderedDictionary
  41.         {
  42.             Console.WriteLine(kvp.Key);
  43.             int sum = kvp.Value.Values.Sum();
  44.             foreach (var wins in kvp.Value.Values)
  45.             {
  46.                 Console.WriteLine($"- Wins: {sum}");
  47.                 break;
  48.             }
  49.             Console.WriteLine($"- Opponents: {string.Join(", ", kvp.Value.Keys.OrderBy(x => x))}");
  50.         }
  51.     }
  52.  
  53.     private static void FilloutAllTeams(Dictionary<string, Dictionary<string, int>> champLeague, string team1, string team2, int team1Goals, int team2Goals)
  54.     {
  55.         if (team1Goals == team2Goals || team1Goals > team2Goals)
  56.         {
  57.             if (!champLeague.ContainsKey(team1))
  58.             {
  59.                 champLeague.Add(team1, new Dictionary<string, int>());
  60.                 champLeague[team1].Add(team2, 1);
  61.             }
  62.             else
  63.             {
  64.                 if (champLeague[team1].ContainsKey(team2))
  65.                 {
  66.                     champLeague[team1][team2] += 1;
  67.                 }
  68.                 else
  69.                 {
  70.                     champLeague[team1].Add(team2, 1);
  71.                 }
  72.  
  73.             }
  74.         }
  75.         if(team1Goals == team2Goals || team1Goals > team2Goals)
  76.         {
  77.             if (!champLeague.ContainsKey(team2))
  78.             {
  79.                 champLeague.Add(team2, new Dictionary<string, int>());
  80.                 champLeague[team2].Add(team1, 0);
  81.             }
  82.             else
  83.             {
  84.                 if (champLeague[team2].ContainsKey(team1))
  85.                 {
  86.                     champLeague[team2][team1] += 0;
  87.                 }
  88.                 else
  89.                 {
  90.                     champLeague[team2].Add(team1, 0);
  91.                 }
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement