Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace OwnClubParty
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int maxCapacity = int.Parse(Console.ReadLine());
  12.             string[] input = Console.ReadLine().Split(" ");
  13.  
  14.             Stack<string> elements = new Stack<string>(input);
  15.             Queue<string> halls = new Queue<string>();
  16.             List<int> allGroups = new List<int>();
  17.  
  18.             int currentCapacity = 0;
  19.  
  20.             while (elements.Any())
  21.             {
  22.                 string currentElement = elements.Pop();
  23.  
  24.                 bool isNumber = int.TryParse(currentElement, out int parsedNumber);
  25.  
  26.                 if (!isNumber)
  27.                 {
  28.                     halls.Enqueue(currentElement);
  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(", ", allGroups)}");
  40.                         allGroups.Clear();
  41.                         currentCapacity = 0;
  42.                     }
  43.                     if (halls.Any())
  44.                     {
  45.                         allGroups.Add(parsedNumber);
  46.                         currentCapacity += parsedNumber;
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement