Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _07.Population_Counter
- {
- class Program
- {
- static void Main(string[] args)
- {
- var countriesPopulation = new Dictionary<string, Dictionary<string, long>>();
- var line = Console.ReadLine();
- while (line != "report")
- {
- var array = line.Split('|');
- var country = array[1];
- var city = array[0];
- var people = long.Parse(array[2]);
- if (!countriesPopulation.ContainsKey(country))
- {
- countriesPopulation[country] = new Dictionary<string, long>();
- }
- if (!countriesPopulation[country].ContainsKey(city))
- {
- countriesPopulation[country][city] = 0;
- }
- countriesPopulation[country][city] += people;
- line = Console.ReadLine();
- }
- foreach (var countryCityPeople in countriesPopulation.OrderByDescending(kvp => kvp.Value.Values))
- {
- var country = countryCityPeople.Key;
- Console.WriteLine(country + "(total population: {0})", countriesPopulation[country].Values.Sum());
- foreach (var cityPeople in countriesPopulation[country].OrderByDescending(p => p.Value))
- {
- var city = cityPeople.Key;
- var people = cityPeople.Value;
- Console.WriteLine($"=>{city}: {people}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment