Advertisement
Aborigenius

Camping

Aug 16th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 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 Camping
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.  
  15.             Dictionary<string, Dictionary<string, int>> campers =
  16.               new Dictionary<string, Dictionary<string, int>>();
  17.  
  18.             while (input != "end")
  19.             {
  20.                 string[] inputTokens = input.Split(' ');
  21.  
  22.                 string name = inputTokens[0];
  23.                 string rvModel = inputTokens[1];
  24.                 int nights = int.Parse(inputTokens[2]);
  25.  
  26.                 if (!campers.ContainsKey(name))
  27.                 {
  28.                     campers[name] = new Dictionary<string, int>();
  29.                 }
  30.                 if (campers.ContainsKey(name))
  31.                 {
  32.                     campers[name][rvModel] = nights;
  33.                 }
  34.                 input = Console.ReadLine();
  35.             }
  36.             foreach (var item in campers.OrderByDescending(x => x.Value.Count()).ThenBy(x => x.Key.Length))
  37.             {
  38.                 Console.WriteLine($"{item.Key}: {item.Value.Count}");
  39.                 int totalNights = 0;
  40.                 foreach (var camper in item.Value)
  41.                 {
  42.                     totalNights += camper.Value;
  43.                     Console.WriteLine("***{0}", camper.Key);
  44.                    
  45.                 }
  46.                 Console.WriteLine($"Total stay: {totalNights} nights");
  47.             }
  48.         }
  49.     }
  50. }
  51. /*Print all the people, ordered by the count of their campers.
  52.  * If two people have an equal count of campers, order them by the length of their names in ascending order.
  53.  * Print the campers for each person in order of receiving.
  54.  * */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement