Advertisement
krasi1105

11.Dragon Army

Mar 4th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 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. namespace dragons_army
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             Dictionary<string, SortedDictionary<string, Dragon>> data = ReadInput(n);
  15.             PrintOutput(data);
  16.         }
  17.  
  18.         private static void PrintOutput(Dictionary<string, SortedDictionary<string, Dragon>> data)
  19.         {
  20.             foreach (var item in data)
  21.             {
  22.                 Dragon[] values = item.Value.Values.ToArray();
  23.                 double damageAvg = GetAverage(values, "damage");
  24.                 double healthAvg = GetAverage(values, "health");
  25.                 double armorAvg = GetAverage(values, "armor");
  26.                 Console.WriteLine($"{item.Key}::({damageAvg:f2}/{healthAvg:f2}/{armorAvg:f2})");
  27.                 foreach (var item2 in item.Value)
  28.                 {
  29.                     Console.WriteLine($"-{item2.Key} -> damage: {item2.Value.Damage}, health: {item2.Value.Health}, armor: {item2.Value.Armor}");
  30.                 }
  31.             }
  32.         }
  33.  
  34.         private static double GetAverage(Dragon[] values, string v)
  35.         {
  36.             int average = 0;
  37.             if (v == "damage")
  38.             {
  39.                 foreach (var item in values)
  40.                 {
  41.                     average += item.Damage;
  42.                 }
  43.             }
  44.             else if (v == "health")
  45.             {
  46.                 foreach (var item in values)
  47.                 {
  48.                     average += item.Health;
  49.                 }
  50.             }
  51.             else
  52.             {
  53.                 foreach (var item in values)
  54.                 {
  55.                     average += item.Armor;
  56.                 }
  57.             }
  58.             return (double)average / values.Length;
  59.         }
  60.  
  61.         private static Dictionary<string, SortedDictionary<string, Dragon>> ReadInput(int n)
  62.         {
  63.             Dictionary<string, SortedDictionary<string, Dragon>> output = new Dictionary<string, SortedDictionary<string, Dragon>>();
  64.             for (int i = 0; i < n; i++)
  65.             {
  66.                 string[] input = Console.ReadLine().Split();
  67.                 string type = input[0];
  68.                 string dragonName = input[1];
  69.                 string[] stats = input.Skip(2).ToArray();
  70.                 int[] DHA = ValidateStats(stats); // damage,health,armor
  71.                 var theNewDragon = new Dragon(DHA);
  72.                 if (output.ContainsKey(type))
  73.                 {
  74.                     if (output[type].ContainsKey(dragonName))
  75.                     {
  76.                         output[type][dragonName] = theNewDragon;
  77.                     }
  78.                     else
  79.                     {
  80.                         output[type].Add(dragonName, theNewDragon);
  81.                     }
  82.                 }
  83.                 else
  84.                 {
  85.                     output.Add(type, new SortedDictionary<string, Dragon> { { dragonName, theNewDragon } });
  86.                 }
  87.             }
  88.             return output;
  89.         }
  90.  
  91.         private static int[] ValidateStats(string[] stats)
  92.         {
  93.             int[] statNumbers = new int[stats.Length];
  94.             int[] defaults = new int[3] { 45, 250, 10 };
  95.             for (int i = 0; i < stats.Length; i++)
  96.             {
  97.                 if (stats[i] == "null")
  98.                 {
  99.                     statNumbers[i] = defaults[i];
  100.                 }
  101.                 else
  102.                 {
  103.                     statNumbers[i] = int.Parse(stats[i]);
  104.                 }
  105.             }
  106.             return statNumbers;
  107.         }
  108.     }
  109.  
  110.     class Dragon
  111.     {
  112.         public int Damage { get; set; }
  113.         public int Health { get; set; }
  114.         public int Armor { get; set; }
  115.  
  116.         public Dragon(int[] DHA)
  117.         {
  118.             Damage = DHA[0];
  119.             Health = DHA[1];
  120.             Armor = DHA[2];
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement