gabi11

C# Adv.(Exam 24022019) - 01. Club Party

Jun 22nd, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ClubParty
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int maxCapacity = int.Parse(Console.ReadLine());
  11.             var inputLine = Console.ReadLine().Split();
  12.  
  13.             Stack<string> elements = new Stack<string>(inputLine);
  14.             Queue<string> halls = new Queue<string>();
  15.             List<int> reservations = new List<int>();
  16.  
  17.             int currentCapacity = 0;
  18.  
  19.             while (elements.Count > 0)
  20.             {
  21.                 string currentElemet = elements.Pop();
  22.  
  23.                 bool isNumber = int.TryParse(currentElemet, out int parsedNumber);
  24.  
  25.                 if (!isNumber)
  26.                 {
  27.                     halls.Enqueue(currentElemet);
  28.                 }
  29.  
  30.                 else
  31.                 {
  32.                     if (halls.Count == 0)
  33.                     {
  34.                         continue;
  35.                     }
  36.  
  37.                     if (currentCapacity + parsedNumber > maxCapacity)
  38.                     {
  39.                         Console.WriteLine($"{halls.Dequeue()} -> {string.Join(", ", reservations)}");
  40.                         reservations.Clear();
  41.                         currentCapacity = 0;
  42.                     }
  43.  
  44.                     if (halls.Count > 0)
  45.                     {
  46.                         reservations.Add(parsedNumber);
  47.                         currentCapacity += parsedNumber;
  48.  
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment