Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I. Associative Arrays
- 01.Count Real Numbers
- Read a list of integers and print them in ascending order, along with their number of occurrences.
- Examples
- Input Output Input Output Input Output
- 8 2 2 8 2 2 -> 3 1 5 1 3 1 -> 2 -2 0 0 2 -2 -> 1
- 8 -> 2 3 -> 1 0 -> 2
- 5 -> 1 2 -> 1
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _01CountRealNumbers
- {
- class Program
- {
- static void Main(string[] args)
- {
- var numbers = Console.ReadLine().Split().Select(double.Parse).ToArray();
- var counts = new SortedDictionary<double, int>();
- //<Key, Value>();
- foreach (var item in numbers)
- {
- if (counts.ContainsKey(item))//Проверява се чрез метода "ContainsKey()", дали
- { //елемента "item" от масива "numbers" се повтаря.
- counts[item]++; //Ако е "true" се увеличава "Value"(стойността)
- }
- else
- {
- counts.Add(item, 1); //Ако е "false" се добавя "Key"(ключ) чрез метода "Add()"
- } //към асоциативния масив(речника) "counts".
- }
- foreach (var item in counts)
- {
- Console.WriteLine($"{item.Key} -> {item.Value}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment