Advertisement
gabi11

Sets & Dictionaries Advanced - 4. Cities by Continent and Co

May 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var cities = new Dictionary<string, Dictionary<string, List<string>>>();
  14.  
  15.             var number = int.Parse(Console.ReadLine());
  16.  
  17.             for (int i = 0; i < number; i++)
  18.             {
  19.                 var input = Console.ReadLine().Split();
  20.  
  21.                 var continent = input[0];
  22.                 var country = input[1];
  23.                 var city = input[2];
  24.  
  25.                 if (!cities.ContainsKey(continent))
  26.                 {
  27.                     cities[continent] = new Dictionary<string, List<string>>();
  28.                 }
  29.  
  30.                 if (!cities[continent].ContainsKey(country))
  31.                 {
  32.                     cities[continent][country] = new List<string>();
  33.                 }
  34.  
  35.                 cities[continent][country].Add(city);
  36.             }
  37.  
  38.             foreach (var continent in cities)
  39.             {
  40.                 var continentName = continent.Key;
  41.                 Console.WriteLine($"{continentName}:");
  42.  
  43.                 foreach (var country in continent.Value)
  44.                 {
  45.                     var countryName = country.Key;
  46.                     var citiesNames = country.Value;
  47.                     Console.WriteLine($"{countryName} -> {string.Join(", ", citiesNames)}");
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement