Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Numerics;
- using System.Text;
- class PopulationCounter
- {
- static void Main()
- {
- var populationStructure = new Dictionary<string, Dictionary<string, int>>();
- string inputline = Console.ReadLine();
- while (inputline != "report")
- {
- string[] tokens = inputline.Split('|');
- string city = tokens[0];
- string country = tokens[1];
- int population = int.Parse(tokens[2]);
- if (populationStructure.ContainsKey(country))
- {
- if (populationStructure[country].ContainsKey(city))
- {
- populationStructure[country][city] += population;
- }
- else
- {
- populationStructure[country][city] = population;
- }
- }
- else
- {
- populationStructure[country] = new Dictionary<string, int>();
- populationStructure[country][city] = population;
- }
- inputline = Console.ReadLine();
- }
- var sortedCountries =
- from country in populationStructure
- orderby TotatCountryPopulation(country.Value) descending
- select country;
- foreach (var countryKeyValuePair in sortedCountries)
- {
- var totalPopulation = TotatCountryPopulation(countryKeyValuePair.Value);
- Console.WriteLine($"{countryKeyValuePair.Key} (total population: {totalPopulation})");
- var sortedCities =
- from city in countryKeyValuePair.Value
- orderby city.Value descending
- select city;
- foreach (var cityKeyValuePair in sortedCities)
- {
- Console.WriteLine($"=>{cityKeyValuePair.Key}: {cityKeyValuePair.Value}");
- }
- }
- }
- public static BigInteger TotatCountryPopulation(Dictionary<string, int> innerDictionary)
- {
- BigInteger result = 0;
- foreach (var keyValuePair in innerDictionary)
- {
- result += keyValuePair.Value;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement