以下是使用C++顺序表实现的代码示例:
#include <iostream>
#include <vector>
class BarberShop {
public:
void initializeBarbers(int n) {
for (int i = 0; i < n; i++) {
barbers.push_back(101 + i); // 初始化理发师编号,从101开始递增
}
}
void assignBarber(int barberNumber) {
if (barberNumber == 0 || !isBarberAvailable(barberNumber)) {
barberNumber = barbers.front(); // 若未指定或指定理发师不可用,则由排班表第一位服务
}
std::cout << "Assigning barber: " << barberNumber << std::endl;
barbers.erase(barbers.begin()); // 移除已安排的理发师
barbers.push_back(barberNumber); // 添加至排班表末尾
}
void finishBarber(int barberNumber) {
std::cout << "Finishing barber: " << barberNumber << std::endl;
auto it = std::find(barbers.begin(), barbers.end(), barberNumber);
if (it != barbers.end()) {
barbers.erase(it);
}
}
void printSchedule() {
for (int i : barbers) {
std::cout << i << "-";
}
std::cout << std::endl;
}
private:
bool isBarberAvailable(int barberNumber) {
return std::find(barbers.begin(), barbers.end(), barberNumber) != barbers.end();
}
std::vector<int> barbers; // 存储理发师列表
};
int main() {
int n;
std::cin >> n;
BarberShop shop;
shop.initializeBarbers(10); // 初始化理发师
for (int i = 0; i < n; i++) {
std::string operation;
int barberNumber;
std::cin >> operation;
std::cin >> barberNumber;
if (operation == "assign") {
shop.assignBarber(barberNumber);
} else if (operation == "finish") {
shop.finishBarber(barberNumber);
}
shop.printSchedule();
}
return 0;
}
输入示例:
5
assign 0
assign 0
assign 105
assign 105
finish 102
输出示例:
102-103-104-105-106-107-108-109-110-
103-104-105-106-107-108-109-110-101-
103-104-106-107-108-109-110-
104 106 107 108 109 110
104 106 107 108 109 110 102
内容由零声教学AI助手提供,问题来源于学员提问




