Advertisement
Guest User

4 zad

a guest
Aug 22nd, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace exam
  7. {
  8.     class exam
  9.     {
  10.         static void Main()
  11.         {
  12.             var trains = new Dictionary<string, Dictionary<string, int>>();
  13.  
  14.             string input = Console.ReadLine();
  15.            
  16.             while (input != "It's Training Men!")
  17.             {
  18.                 string[] myArr = input.Split();
  19.  
  20.                 string trainName = myArr[0];
  21.  
  22.                 if (myArr.Length == 5)
  23.                 {
  24.                     string wagonName = myArr[2];
  25.                     int wagonPower = int.Parse(myArr[4]);
  26.  
  27.                     if (!trains.ContainsKey(trainName))
  28.                     {
  29.                         trains.Add(trainName, new Dictionary<string, int>());
  30.                     }
  31.                     if (!trains[trainName].ContainsKey(wagonName))
  32.                     {
  33.                         trains[trainName].Add(wagonName, 0);
  34.                     }
  35.                     trains[trainName][wagonName] += wagonPower;
  36.                 }
  37.                
  38.                 else
  39.                 {
  40.                     string command = myArr[1];
  41.                     string otherTrain = myArr[2];
  42.  
  43.                     switch (command)
  44.                     {
  45.                         case "->":
  46.                             if (!trains.ContainsKey(trainName))
  47.                             {
  48.                                 trains.Add(trainName, new Dictionary<string, int>());
  49.                             }
  50.  
  51.                             foreach (var tr2 in trains[otherTrain])
  52.                             {
  53.                                 trains[trainName].Add(tr2.Key, tr2.Value);
  54.                             }
  55.  
  56.                             trains.Remove(otherTrain);
  57.  
  58.                             break;
  59.  
  60.                         case "=":
  61.                             if (!trains.ContainsKey(trainName))
  62.                             {
  63.                                 trains.Add(trainName, new Dictionary<string, int>());
  64.                             }
  65.  
  66.                             trains[trainName].Clear();
  67.  
  68.                             foreach (var tr2 in trains[otherTrain])
  69.                             {
  70.                                 trains[trainName].Add(tr2.Key, tr2.Value);
  71.                             }
  72.  
  73.                             break;
  74.                     }
  75.  
  76.                 }
  77.  
  78.                 input = Console.ReadLine();
  79.             }
  80.  
  81.            
  82.             foreach (var train in trains
  83.                 .OrderByDescending(p => p.Value.Values.Sum())
  84.                 .ThenBy(c => c.Value.Count())
  85.                 )
  86.             {
  87.                 Console.WriteLine($"Train: {train.Key}");
  88.  
  89.                 foreach (var wagon in train.Value
  90.                     .OrderByDescending(pp => pp.Value)
  91.                     )
  92.                 {
  93.                     Console.WriteLine($"###{wagon.Key} - {wagon.Value}");
  94.                 }
  95.             }
  96.  
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement