JulianJulianov

07.AssociativeArray - A Miner Task

Apr 12th, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. 07. A Miner Task
  2. You will be given a sequence of strings, each on a new line. Every odd line on the console is representing a resource (e.g. Gold, Silver, Copper, and so on) and every even - quantity. Your task is to collect the resources and print them each on a new line.
  3. Print the resources and their quantities in the following format:
  4. {resource}> {quantity}
  5. The quantities will be in the range [12 000 000 000]
  6. Examples
  7. Input                     Output                          Input                           Output
  8. Gold                      Gold -> 155                     gold                            gold -> 170
  9. 155                       Silver -> 10                    155                             silver -> 10
  10. Silver                    Copper -> 17                    silver                          copper -> 17
  11. 10                                                        10
  12. Copper                                                    copper
  13. 17                                                        17
  14. stop                                                      gold
  15.                                                           15
  16.                                                           stop
  17. using System;
  18. using System.Collections.Generic;
  19.  
  20. namespace _07A_MinerTask
  21. {
  22.     class Program
  23.     {
  24.         static void Main(string[] args)
  25.         {
  26.             var resourse = "";
  27.             var minerTask = new Dictionary<string, int>();
  28.            
  29.  
  30.             while (!(resourse = Console.ReadLine()).Equals("stop"))
  31.             {
  32.                 var quantity = Console.ReadLine();
  33.                 var list = new List<string>();
  34.                 list.Add(resourse);
  35.                 list.Add(quantity);
  36.                 if (minerTask.ContainsKey(list[0]))
  37.                 {
  38.                     minerTask[list[0]] += int.Parse(list[1]);
  39.                 }
  40.                 else
  41.                 {
  42.                     minerTask.Add(list[0], int.Parse(list[1]));
  43.                 }
  44.             }
  45.             foreach (var item in minerTask)
  46.             {
  47.                 Console.WriteLine($"{item.Key} -> {item.Value}");
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment