Advertisement
fbinnzhivko

Untitled

Jun 13th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         string key = Console.ReadLine();
  9.         SortedDictionary<string, long> standings = new SortedDictionary<string, long>();
  10.         SortedDictionary<string, long> goalScored = new SortedDictionary<string, long>();
  11.         while (true)
  12.         {
  13.             string matches = Console.ReadLine();
  14.             if (matches == "final") { break; }
  15.  
  16.             int startFirstTeam = matches.IndexOf(key);
  17.             int endFirstTeam = matches.IndexOf(key, startFirstTeam + key.Length);
  18.             int startSecondTeam = matches.IndexOf(key, endFirstTeam + key.Length);
  19.             int endSecondTeam = matches.IndexOf(key, startSecondTeam + key.Length);
  20.             string firstTeam = matches.Substring(startFirstTeam + key.Length, endFirstTeam - startFirstTeam - key.Length).ToUpper();
  21.             string secondTeam = matches.Substring(startSecondTeam + key.Length, endSecondTeam - startSecondTeam - key.Length).ToUpper();
  22.             firstTeam = Reverse(firstTeam);
  23.             secondTeam = Reverse(secondTeam);
  24.  
  25.             string[] splitString = matches.Split(' ').ToArray();
  26.             int[] matchResult = splitString.Last().Split(':').Select(int.Parse).ToArray();
  27.             int firstResult = matchResult[0];
  28.             int secondResult = matchResult[1];
  29.             standings = ContainsFirst(standings, firstTeam, firstResult, secondResult);
  30.             standings = ContainsSecond(standings, secondTeam, firstResult, secondResult);
  31.             goalScored = GoalScoredFirst(goalScored, firstTeam, firstResult);
  32.             goalScored = GoalScoredSecond(goalScored, secondTeam, secondResult);
  33.         }
  34.         int position = 1;
  35.         Console.WriteLine("League standings:");
  36.         foreach (var team in standings.OrderByDescending(x => x.Value))
  37.         {
  38.             Console.WriteLine("{0}. {1} {2}", position, team.Key, team.Value);
  39.             position++;
  40.         }
  41.         Console.WriteLine("Top 3 scored goals:");
  42.         foreach (var team in goalScored.OrderByDescending(x => x.Value).Take(3))
  43.         {
  44.             Console.WriteLine("- {0} -> {1}", team.Key, team.Value);
  45.         }
  46.     }
  47.     static string Reverse(string team)
  48.     {
  49.         char[] teamArray = team.ToArray();
  50.         Array.Reverse(teamArray);
  51.         string reversed = new string(teamArray);
  52.         return reversed;
  53.     }
  54.     static SortedDictionary<string, long> GoalScoredFirst(SortedDictionary<string, long> teams, string firstTeam, int firstResult)
  55.     {
  56.         if (teams.ContainsKey(firstTeam))
  57.         {
  58.             teams[firstTeam] += firstResult;
  59.         }
  60.         else
  61.         {
  62.             teams[firstTeam] = firstResult;
  63.         }
  64.         return teams;
  65.     }
  66.     static SortedDictionary<string, long> GoalScoredSecond(SortedDictionary<string, long> teams, string secondTeam, int secondResult)
  67.     {
  68.         if (teams.ContainsKey(secondTeam))
  69.         {
  70.             teams[secondTeam] += secondResult;
  71.         }
  72.         else
  73.         {
  74.             teams[secondTeam] = secondResult;
  75.         }
  76.         return teams;
  77.     }
  78.     static SortedDictionary<string, long> ContainsFirst(SortedDictionary<string, long> dicitionary, string firstTeam, int firstResult, int secondResult)
  79.     {
  80.         if (dicitionary.ContainsKey(firstTeam))
  81.         {
  82.             if (firstResult > secondResult)
  83.             { dicitionary[firstTeam] += 3; }
  84.             else if (firstResult == secondResult)
  85.             { dicitionary[firstTeam]++; }
  86.         }
  87.         else if (!dicitionary.ContainsKey(firstTeam))
  88.         {
  89.             if (firstResult > secondResult)
  90.             { dicitionary[firstTeam] = 3; }
  91.             else if (firstResult == secondResult)
  92.             { dicitionary[firstTeam] = 1; }
  93.             else if (firstResult < secondResult)
  94.             { dicitionary[firstTeam] = 0; }
  95.         }
  96.         return dicitionary;
  97.     }
  98.     static SortedDictionary<string, long> ContainsSecond(SortedDictionary<string, long> dicitionary, string secondTeam, int firstResult, int secondResult)
  99.     {
  100.         if (dicitionary.ContainsKey(secondTeam))
  101.         {
  102.             if (firstResult < secondResult)
  103.             { dicitionary[secondTeam] += 3; }
  104.             else if (firstResult == secondResult)
  105.             { dicitionary[secondTeam]++; }
  106.         }
  107.         else if (!dicitionary.ContainsKey(secondTeam))
  108.         {
  109.             if (firstResult < secondResult)
  110.             { dicitionary[secondTeam] = 3; }
  111.             else if (firstResult == secondResult)
  112.             { dicitionary[secondTeam] = 1; }
  113.             else if (firstResult > secondResult)
  114.             { dicitionary[secondTeam] = 0; }
  115.         }
  116.         return dicitionary;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement