Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CitiesByContinentAndCountry
- {
- class StartUp
- {
- static void Main()
- {
- List<Continent> continents = new List<Continent>();
- int n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- string[] command = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- string continent = command[0];
- string country = command[1];
- string city = command[2];
- if (!continents.Any(x=>x.ContinentName == continent))
- {
- var currentContinent = new Continent(continent, new Country(country));
- currentContinent.Countries.Add(city)
- continents.Add(currentContinent);
- }
- else
- {
- var target = continents.FirstOrDefault(x => x.ContinentName == continent);
- if (target.Country.CountryName == country)
- {
- target.Country.City.Add(city);
- }
- else
- {
- var currentContinent = new Continent(continent, new Country(country));
- currentContinent.Country.City.Add(city);
- continents.Add(currentContinent);
- }
- }
- }
- foreach (var item in continents)
- {
- Console.WriteLine(item.ContinentName);
- Console.WriteLine($"{item.Country.CountryName} -> {string.Join(" ", item.Country.City)}");
- }
- }
- public class Continent
- {
- public Continent(string continentName, Country country)
- {
- this.ContinentName = continentName;
- this.Countries = new HashSet<Country>();
- }
- public HashSet<Country> Countries { get; set; }
- public string ContinentName { get; set; }
- public void Add(Country country)
- {
- Countries.Add(country);
- }
- }
- public class Country
- {
- public Country(string countryName)
- {
- CountryName = countryName;
- City = new HashSet<string>();
- }
- public string CountryName { get; set; }
- public HashSet<string> City { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement