Advertisement
Ivakis

Population Counter

Oct 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace P07_PopulationCenter
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var populationCounter = new Dictionary<string, Dictionary<string, long>>(); // country, city, population
  14.  
  15. for (int i = 0; ; i++)
  16. {
  17. var input = Console.ReadLine().Split('|');
  18.  
  19. if (input[0] == "report")
  20. {
  21. break;
  22. }
  23.  
  24. var country = input[1];
  25. var city = input[0];
  26. var population = int.Parse(input[2]);
  27.  
  28.  
  29.  
  30. if (!populationCounter.ContainsKey(country))
  31. {
  32. populationCounter.Add(country, new Dictionary<string, long>());
  33. }
  34.  
  35. if (!populationCounter[country].ContainsKey(city))
  36. {
  37. populationCounter[country][city] = 0;
  38. }
  39.  
  40. populationCounter[country][city] += population;
  41. }
  42.  
  43. foreach (KeyValuePair<string, Dictionary<string, long>> country in populationCounter.OrderByDescending(a => a.Value.Values.Sum()))
  44. {
  45.  
  46. Console.WriteLine($"{country.Key} (total population: {country.Value.Select(a => a.Value).Sum()})");
  47.  
  48. foreach (KeyValuePair<string, long> city in country.Value.OrderByDescending(a => a.Value))
  49. {
  50. Console.WriteLine($"=>{city.Key}: {city.Value}");
  51. }
  52. }
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement