Created
November 27, 2023 02:21
-
-
Save unilecs/a3ad79b5152a954b222fbf8fe0fe55fd to your computer and use it in GitHub Desktop.
Задача по дизайну: Система для бронирования мест
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public class Program | |
{ | |
public class SeatReserver | |
{ | |
private int N; | |
private PriorityQueue<int, int> availableSeats; | |
public SeatReserver(int n) { | |
N = n; | |
availableSeats = new PriorityQueue<int, int>(); | |
for (int i = 1; i <= n; i++) | |
{ | |
availableSeats.Enqueue(i, i); | |
} | |
} | |
public int Reserve() { | |
if (availableSeats.Count == 0) | |
{ | |
throw new Exception("There are no more available seats."); | |
} | |
return availableSeats.Dequeue(); | |
} | |
public void Unreserve(int num) { | |
if (num > N) | |
{ | |
throw new Exception("Num is larger than the max size N."); | |
} | |
availableSeats.Enqueue(num, num); | |
} | |
} | |
private static void Print(int n) | |
{ | |
Console.WriteLine(n.ToString()); | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
// tests | |
var seatReserver = new SeatReserver(5); | |
Print(seatReserver.Reserve()); // 1 | |
Print(seatReserver.Reserve()); // 2 | |
seatReserver.Unreserve(2); // 2е место снова доступно | |
Print(seatReserver.Reserve()); // 2 | |
Print(seatReserver.Reserve()); // 3 | |
Print(seatReserver.Reserve()); // 4 | |
Print(seatReserver.Reserve()); // 5 | |
Print(seatReserver.Reserve()); // Exception: There are no more free seats. | |
seatReserver.Unreserve(6); // Exception: Num is larger than the max size N. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment