Militsa

03. Cities by Continent and Country

Dec 7th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 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 _03.Cities_by_Continent_and_Country
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var n = int.Parse(Console.ReadLine());
  14.  
  15.             var continents = new Dictionary<string, Dictionary<string, List<string>>>();
  16.  
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 var tokens = Console.ReadLine().Split();
  20.                 var continent = tokens[0];
  21.                 var country = tokens[1];
  22.                 var city = tokens[2];
  23.  
  24.                 if (!continents.ContainsKey(continent))
  25.                 {
  26.                     continents[continent] = new Dictionary<string,List<string>>();
  27.                 }
  28.                 if (!continents[continent].ContainsKey(country))
  29.                 {
  30.                     continents[continent][country] = new List<string>();
  31.                 }
  32.                 continents[continent][country].Add(city);
  33.             }
  34.  
  35.             foreach (var continentCountry in continents)
  36.             {
  37.                 var continentName = continentCountry.Key;
  38.                 var countries = continentCountry.Value;
  39.                 Console.WriteLine("{0}:", continentName);
  40.  
  41.                 foreach (var countryCity in countries)
  42.                 {
  43.                     var countryName = countryCity.Key;
  44.                     var cities = countryCity.Value;
  45.  
  46.                     Console.WriteLine("  " + countryName + " -> "  + string.Join(", ", cities));
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Add Comment
Please, Sign In to add comment