Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. namespace _04.PopulationCounter
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. class PopulationCounter
  8. {
  9. static Dictionary<string, Dictionary<string, long>> database = new Dictionary<string, Dictionary<string, long>>();
  10.  
  11. static void Main()
  12. {
  13. while (true)
  14. {
  15. string input = Console.ReadLine();
  16. if (input == "report") break;
  17.  
  18. string[] tokens = input.Split('|');
  19.  
  20. string town = tokens[0];
  21. string country = tokens[1];
  22. long population = long.Parse(tokens[2]);
  23.  
  24. if (!database.ContainsKey(country))
  25. {
  26. database.Add(country, new Dictionary<string, long>());
  27. }
  28.  
  29. if (!database[country].ContainsKey(town))
  30. {
  31. database[country].Add(town, 0);
  32. }
  33.  
  34. database[country][town] += population;
  35. }
  36.  
  37. Print();
  38. }
  39.  
  40. private static void Print()
  41. {
  42. foreach (var country in database.OrderByDescending(c => c.Value.Sum(t => t.Value)))
  43. {
  44. Console.WriteLine("{0} (total population: {1})", country.Key, country.Value.Sum(t => t.Value));
  45. foreach (var town in country.Value.OrderByDescending(c => c.Value))
  46. {
  47. Console.WriteLine("=>{0}: {1}", town.Key, town.Value);
  48. }
  49. }
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement