Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _03NestedDictionariesExcercisesTravelCompany
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> destination = new List<string>();
- List<int> groupCount = new List<int>();
- Dictionary<string, Dictionary<string, int>> TravelingMethods = new Dictionary<string, Dictionary<string, int>>();
- string entryInfo = Console.ReadLine();
- while (entryInfo != "ready")
- {
- string[] input = entryInfo.Split(':').ToArray();
- string town = input[0];
- string[] transportoptions = input[1].Split('-', ',').ToArray();
- for (int i = 0; i < transportoptions.Length; i += 2)
- {
- string transport = transportoptions[i];
- int amountofTravelers = int.Parse(transportoptions[i + 1]);
- if(!TravelingMethods.ContainsKey(town))
- {
- TravelingMethods.Add(town, new Dictionary<string, int>());
- TravelingMethods[town].Add(transport, amountofTravelers);
- }
- else if (TravelingMethods[town].ContainsKey(transport))
- {
- TravelingMethods[town][transport] = amountofTravelers;
- }
- else
- {
- TravelingMethods[town].Add(transport, amountofTravelers);
- }
- }
- entryInfo = Console.ReadLine();
- }
- entryInfo = Console.ReadLine();
- while(entryInfo!="travel time!")
- {
- string[] travelingDestinations = entryInfo.Split(' ').ToArray();
- string destinationTown = travelingDestinations[0];
- int totalgroupnumbers = int.Parse(travelingDestinations[1]);
- destination.Add(destinationTown);
- groupCount.Add(totalgroupnumbers);
- entryInfo = Console.ReadLine();
- }
- for(int k=0;k<destination.Count;k++)
- {
- if (TravelingMethods.ContainsKey(destination[k]))
- {
- Dictionary<string, int> acomodations = TravelingMethods[destination[k]];
- string town = destination[k];
- int grouptotal = groupCount[k];
- int totalplaces = 0;
- foreach (int num in acomodations.Values)
- {
- totalplaces += num;
- }
- if(totalplaces-grouptotal>=0)
- {
- Console.WriteLine($"{town} -> all {grouptotal} accommodated");
- }
- else
- {
- Console.WriteLine("{0} -> all except {1} accommodated",town,Math.Abs(totalplaces-grouptotal));
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment