Advertisement
Dimoto

12 June 20 - 3. Football League

Jun 20th, 2016
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 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. class Problem3
  8. {
  9.     static void Main()
  10.     {
  11.         string key = Console.ReadLine();
  12.         Dictionary<string, int> teamPoints = new Dictionary<string, int>();
  13.         Dictionary<string, int> teamGoals = new Dictionary<string, int>();
  14.  
  15.         while (true)
  16.         {
  17.             string info = Console.ReadLine();
  18.  
  19.             if (info == "final")
  20.             {
  21.                 break;
  22.             }
  23.             string[] teamScore = info.Split();
  24.             string teamA = teamScore[0];
  25.             string teamB = teamScore[1];
  26.             var score = teamScore[2].Split(':');
  27.  
  28.             int teamAGoals = int.Parse(score[0]);
  29.             int teamBGoals = int.Parse(score[1]);
  30.  
  31.            
  32.             string teamAname = SubstracTeamName(teamA, key);
  33.             string teamBname = SubstracTeamName(teamB, key);
  34.  
  35.             if (teamGoals.ContainsKey(teamAname))
  36.             {
  37.                 teamGoals[teamAname] += teamAGoals;
  38.             }
  39.             else
  40.             {
  41.                 teamGoals.Add(teamAname, teamAGoals);
  42.             }
  43.             if (teamGoals.ContainsKey(teamBname))
  44.             {
  45.                 teamGoals[teamBname] += teamBGoals;
  46.             }
  47.             else
  48.             {
  49.                 teamGoals.Add(teamBname, teamBGoals);
  50.             }
  51.             // POINTS FOR THE TEAMS
  52.             if (teamAGoals > teamBGoals)
  53.             {
  54.                 if (teamPoints.ContainsKey(teamAname))
  55.                 {
  56.                     int count;
  57.                     teamPoints.TryGetValue(teamAname, out count);
  58.                     teamPoints[teamAname] = count + 3;
  59.                     //teamPoints[teamAname] + 3;
  60.                 }
  61.                 else
  62.                 {
  63.                     teamPoints.Add(teamAname, 3);
  64.                 }
  65.  
  66.                 if (teamPoints.ContainsKey(teamBname))
  67.                 {
  68.                     int count;
  69.                     teamPoints.TryGetValue(teamBname, out count);
  70.                     teamPoints[teamBname] = count + 0;
  71.                     //teamPoints[teamBname] + 0;
  72.                 }
  73.                 else
  74.                 {
  75.                     teamPoints.Add(teamBname, 0);
  76.                 }
  77.             }
  78.             else if (teamAGoals < teamBGoals)
  79.             {
  80.                 if (teamPoints.ContainsKey(teamBname))
  81.                 {
  82.                     int count;
  83.                     teamPoints.TryGetValue(teamBname, out count);
  84.                     teamPoints[teamBname] = count + 3;
  85.                     //teamPoints[teamBname] + 3;
  86.                 }
  87.                 else
  88.                 {
  89.                     teamPoints.Add(teamBname, 3);
  90.                 }
  91.                 if(teamPoints.ContainsKey(teamAname))
  92.                 {
  93.                     int count;
  94.                     teamPoints.TryGetValue(teamAname, out count);
  95.                     teamPoints[teamAname] = count + 0;
  96.                     //teamPoints[teamAname] + 0;
  97.                 }
  98.                 else
  99.                 {
  100.                     teamPoints.Add(teamAname, 0);
  101.                 }
  102.             }
  103.             else
  104.             {
  105.                 if (teamPoints.ContainsKey(teamAname))
  106.                 {
  107.                     int count;
  108.                     teamPoints.TryGetValue(teamAname, out count);
  109.                     teamPoints[teamAname] = count + 1;
  110.                     //teamPoints[teamAname] + 1;
  111.                 }
  112.                 else
  113.                 {
  114.                     teamPoints.Add(teamAname, 1);
  115.                 }
  116.  
  117.                 if (teamPoints.ContainsKey(teamBname))
  118.                 {
  119.                     int count;
  120.                     teamPoints.TryGetValue(teamBname, out count);
  121.                     teamPoints[teamBname] = count + 1;
  122.                     //teamPoints[teamBname] + 1;
  123.                 }
  124.                 else
  125.                 {
  126.                     teamPoints.Add(teamBname, 1);
  127.                 }
  128.  
  129.             }
  130.            //Points for the teams ++++++++++++++++++++++++++++
  131.         }
  132.         Console.WriteLine("League standings:");
  133.         var orderedByPoints = teamPoints.OrderByDescending(team => team.Value).ThenBy(team => team.Key);
  134.         int counter = 1;
  135.         foreach (var team in orderedByPoints)
  136.         {
  137.            
  138.             Console.WriteLine("{0}. {1} {2}", counter, team.Key, team.Value);
  139.             counter++;
  140.         }
  141.  
  142.         Console.WriteLine("Top 3 scored goals:");
  143.         var orderedByGoals = teamGoals.OrderByDescending(team => team.Value).ThenBy(team => team.Key).Take(3);
  144.         foreach (var team in orderedByGoals)
  145.         {
  146.             Console.WriteLine("- {0} -> {1}", team.Key, team.Value);
  147.         }
  148.     }
  149.  
  150.     public static string SubstracTeamName(string team, string key)
  151.     {
  152.         int indexOfKey = team.IndexOf(key);
  153.         string secondPart = team.Substring(indexOfKey + key.Length);
  154.         int secondKey = secondPart.IndexOf(key);
  155.         var teamName = team.Substring(indexOfKey + key.Length, secondKey).ToUpper();
  156.  
  157.         char[] arr = teamName.ToCharArray();
  158.         Array.Reverse(arr);
  159.        
  160.         return new string (arr);
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement