Advertisement
grubcho

Continents - Dictionary<string, Dictionary<string, List<stri

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