Advertisement
LusienGG

[C#] Exam - 03. Football League

Jun 12th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 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.  
  8. class FootballLeague
  9. {
  10.     static void Main()
  11.     {
  12.         string key = Console.ReadLine();
  13.         var standings = new Dictionary<string, long>();
  14.         var scorers = new Dictionary<string, long>();
  15.         while (true)
  16.         {
  17.             string input = Console.ReadLine();
  18.            
  19.             if (input == "final")
  20.             {
  21.                 break;
  22.             }
  23.             var firstStartIndex = input.IndexOf(key) + key.Length;
  24.             var firstEndIndex = input.IndexOf(key, firstStartIndex);
  25.             var firstTeamNameLength = firstEndIndex - firstStartIndex;
  26.             var team1 = input.Substring(firstStartIndex, firstTeamNameLength);
  27.             team1 = Reverse(team1);
  28.             team1 = team1.ToUpper();
  29.             var secondStartIndex = input.IndexOf(key, firstEndIndex + key.Length) + key.Length;
  30.             var secondEndIndex = input.IndexOf(key, secondStartIndex);
  31.             var secondTeamNameLength = secondEndIndex - secondStartIndex;
  32.             var team2 = input.Substring(secondStartIndex, secondTeamNameLength);
  33.             team2 = Reverse(team2);
  34.             team2 = team2.ToUpper();
  35.             string[] splitedInput = input.Split(' ').ToArray();
  36.             string score = splitedInput[2];
  37.             long[] scoreTokens = score.Split(':').Select(long.Parse).ToArray();
  38.             long score1 = scoreTokens[0];
  39.             long score2 = scoreTokens[1];
  40.             long pointsTeam1 = 0;
  41.             long pointsTeam2 = 0;
  42.             if (score1 > score2)
  43.             {
  44.                 pointsTeam1 = 3;
  45.             }
  46.             else if (score1 == score2)
  47.             {
  48.                 pointsTeam1 = 1;
  49.                 pointsTeam2 = 1;
  50.             }
  51.             else
  52.             {
  53.                 pointsTeam2 = 3;
  54.             }
  55.  
  56.             if (standings.ContainsKey(team1))
  57.             {
  58.                 standings[team1] += pointsTeam1;
  59.                 scorers[team1] += score1;
  60.             }
  61.             else
  62.             {
  63.                 standings[team1] = pointsTeam1;
  64.                 scorers[team1] = score1;
  65.             }
  66.  
  67.             if (standings.ContainsKey(team2))
  68.             {
  69.                 standings[team2] += pointsTeam2;
  70.                 scorers[team2] += score2;
  71.             }
  72.             else
  73.             {
  74.                 standings[team2] = pointsTeam2;
  75.                 scorers[team2] = score2;
  76.             }
  77.         }
  78.         standings = standings.OrderBy(x => x.Key).OrderByDescending(x => x.Value).ToDictionary(t => t.Key, t => t.Value);
  79.         scorers = scorers.OrderBy(x => x.Key).OrderByDescending(x => x.Value).Take(3).ToDictionary(t => t.Key, t => t.Value);
  80.         long place = 1;
  81.         Console.WriteLine("League standings:");
  82.         foreach (var team in standings.Keys)
  83.         {
  84.             Console.WriteLine($"{place}. {team} {standings[team]}");
  85.             place++;
  86.         }
  87.         Console.WriteLine("Top 3 scored goals:");
  88.         foreach (var team in scorers.Keys)
  89.         {
  90.             Console.WriteLine($"- {team} -> {scorers[team]}");
  91.         }
  92.  
  93.     }
  94.     public static string Reverse(string s)
  95.     {
  96.         char[] charArray = s.ToCharArray();
  97.         Array.Reverse(charArray);
  98.         return new string(charArray);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement