Guest User

Untitled

a guest
Oct 9th, 2016
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 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.  
  8. class Population_Counter
  9. {
  10. static void Main()
  11. {
  12. Dictionary<string, Dictionary<string, long>> populations = new Dictionary<string, Dictionary<string, long>>();
  13.  
  14. string city = "";
  15. string country = "";
  16. long population = 0;
  17. while (true)
  18. {
  19. List<string> data = Console.ReadLine().Split('|').ToList();
  20. city = data[0];
  21. if (city == "report") break;
  22. country = data[1];
  23. population = long.Parse(data[2]);
  24. Dictionary<string, long> cityPopulation = new Dictionary<string, long>();
  25.  
  26.  
  27.  
  28.  
  29.  
  30. if (!populations.ContainsKey(country))
  31. {
  32.  
  33. cityPopulation[city] = population;
  34.  
  35. populations[country] = cityPopulation;
  36.  
  37.  
  38. }
  39. else
  40. {
  41.  
  42. cityPopulation = populations[country];
  43.  
  44. if (cityPopulation.ContainsKey(city))
  45. cityPopulation[city] += population;
  46. else
  47. cityPopulation.Add(city, population);
  48.  
  49.  
  50. populations[country]=cityPopulation;
  51. }
  52. }
  53.  
  54. foreach (var state in populations.OrderByDescending(x=>x.Value.Sum(y=>y.Value)))
  55. {
  56. List<long> sumOfTowns = state.Value.Select(x => x.Value).ToList();
  57. Console.WriteLine($"{state.Key} (total population: {sumOfTowns.Sum()})");
  58.  
  59. Console.Write($"=>{string.Join("=>", state.Value.OrderByDescending(x=>x.Value).Select(x => $"{x.Key}: {x.Value}\r\n"))}");
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment