Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _04.PopulationCounter
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class PopulationCounter
- {
- static Dictionary<string, Dictionary<string, long>> database = new Dictionary<string, Dictionary<string, long>>();
- static void Main()
- {
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "report") break;
- string[] tokens = input.Split('|');
- string town = tokens[0];
- string country = tokens[1];
- long population = long.Parse(tokens[2]);
- if (!database.ContainsKey(country))
- {
- database.Add(country, new Dictionary<string, long>());
- }
- if (!database[country].ContainsKey(town))
- {
- database[country].Add(town, 0);
- }
- database[country][town] += population;
- }
- Print();
- }
- private static void Print()
- {
- foreach (var country in database.OrderByDescending(c => c.Value.Sum(t => t.Value)))
- {
- Console.WriteLine("{0} (total population: {1})", country.Key, country.Value.Sum(t => t.Value));
- foreach (var town in country.Value.OrderByDescending(c => c.Value))
- {
- Console.WriteLine("=>{0}: {1}", town.Key, town.Value);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement