Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _07.Population_Center
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- var countryWithTown = new Dictionary<string, Dictionary<string, int>>();
- while (input != "report")
- {
- string[] inputInfo = input.Split('|');
- var country = inputInfo[1];
- var town = inputInfo[0];
- int population = int.Parse(inputInfo[2]);
- if (!countryWithTown.ContainsKey(country))
- {
- countryWithTown.Add(country, new Dictionary<string, int>());
- }
- countryWithTown[country].Add(town, population);
- input = Console.ReadLine();
- }
- var countryWithTotalPopulation = new Dictionary<string, int>();
- foreach (var pair in countryWithTown)
- {
- var currentCountry = pair.Key;
- countryWithTotalPopulation.Add(currentCountry, 0);
- foreach (var pair2 in countryWithTown[currentCountry])
- {
- countryWithTotalPopulation[currentCountry] += pair2.Value;
- }
- }
- //all total populations added
- //now we sort them
- foreach (KeyValuePair<string, int> item in countryWithTotalPopulation.OrderByDescending(key => key.Value))
- {
- // do something with item.Key and item.Value
- Console.WriteLine("{0} (total population: {1})",item.Key,item.Value);
- foreach (var townPair in countryWithTown[item.Key].OrderByDescending(key => key.Value))
- {
- Console.WriteLine("=>{0}: {1}",townPair.Key,townPair.Value);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment