Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace TryOut
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- Dictionary<string, Dictionary<string, long>> countries = new Dictionary<string, Dictionary<string, long>>();
- Dictionary<string, long> totalPopulation = new Dictionary<string, long>();
- while (input != "report")
- {
- string[] data = input.Split('|');
- string country = data[1];
- if (countries.ContainsKey(country) == false)
- {
- countries.Add(country, new Dictionary<string, long>());
- }
- Dictionary<string, long> cities = countries[country];
- string city = data[0];
- long population = long.Parse(data[2]);
- cities.Add(city, population);
- if (totalPopulation.ContainsKey(country) == false)
- {
- totalPopulation.Add(country, population);
- }
- else
- {
- totalPopulation[country] += population;
- }
- totalPopulation = totalPopulation.OrderByDescending(c => c.Value).ToDictionary(c => c.Key, c => c.Value);
- input = Console.ReadLine();
- }
- foreach (var country in totalPopulation)
- {
- Console.WriteLine($"{country.Key} (total population: {country.Value})");
- Dictionary<string, long> cities = countries[country.Key];
- cities = cities.OrderByDescending(c => c.Value).ToDictionary(c => c.Key, c => c.Value);
- foreach (var city in cities)
- {
- Console.WriteLine($"=>{city.Key}: {city.Value}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment