Advertisement
Guest User

Untitled

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