JulianJulianov

01.AssociativeArrays-CountRealNumbers

Mar 9th, 2020
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. I.  Associative Arrays
  2. 01.Count Real Numbers
  3. Read a list of integers and print them in ascending order, along with their number of occurrences.
  4. Examples
  5. Input       Output       Input       Output      Input      Output
  6. 8 2 2 8 2   2 -> 3       1 5 1 3     1 -> 2     -2 0 0 2   -2 -> 1
  7.             8 -> 2                   3 -> 1                 0 -> 2
  8.                                      5 -> 1                 2 -> 1
  9.            
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14.  
  15. namespace _01CountRealNumbers
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             var numbers = Console.ReadLine().Split().Select(double.Parse).ToArray();
  22.             var counts = new SortedDictionary<double, int>();
  23.                                             //<Key, Value>();            
  24.             foreach (var item in numbers)
  25.             {
  26.                 if (counts.ContainsKey(item))//Проверява се чрез метода "ContainsKey()", дали
  27.                 {                            //елемента "item" от масива "numbers" се повтаря.
  28.                     counts[item]++;          //Ако е "true" се увеличава "Value"(стойността)
  29.                 }
  30.                 else
  31.                 {
  32.                     counts.Add(item, 1);     //Ако е "false" се добавя "Key"(ключ) чрез метода "Add()"
  33.                 }                            //към асоциативния масив(речника) "counts".
  34.             }
  35.             foreach (var item in counts)
  36.             {
  37.                 Console.WriteLine($"{item.Key} -> {item.Value}");
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment