desislava_topuzakova

03. Miner Task

Mar 27th, 2022
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _03._Miner_Task
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Dictionary<string, int> materialsQuantity = new Dictionary<string, int>();
  11.             //материал -> количество
  12.             while (true)
  13.             {
  14.                 string material = Console.ReadLine();
  15.                 if (material == "stop")
  16.                 {
  17.                     break;
  18.                 }
  19.                 //валиден материал
  20.                 int quantity = int.Parse(Console.ReadLine());
  21.  
  22.                 if (!materialsQuantity.ContainsKey(material))
  23.                 {
  24.                     //нямаме до момента такъв материал
  25.                     materialsQuantity.Add(material, quantity);
  26.                 }
  27.                 else
  28.                 {
  29.                     //имаме такъв материал
  30.                     int currentQuantity = materialsQuantity[material];
  31.                     materialsQuantity[material] = currentQuantity + quantity;
  32.                 }
  33.  
  34.             }
  35.  
  36.             foreach (KeyValuePair<string, int> record in materialsQuantity)
  37.             {
  38.                 //{resource} –> {quantity}
  39.                 Console.WriteLine(record.Key + " -> " + record.Value);
  40.             }
  41.         }
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment