设计停车系统
问题:
请你给一个停车场设计一个停车系统。停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位。
请你实现 ParkingSystem 类:
ParkingSystem(int big, int medium, int small)
初始化ParkingSystem
类,三个参数分别对应每种停车位的数目。bool addCar(int carType)
检查是否有 carType 对应的停车位。 carType 有三种类型:大,中,小,分别用数字 1, 2 和 3 表示。一辆车只能停在 carType 对应尺寸的停车位中。如果没有空车位,请返回false
,否则将该车停入车位并返回true
。
思路:
- 用一个数组保存三种不同的停车位
- 在构造函数中构造各种停车位数
- 判断停车位是否满并让停车位减一
class ParkingSystem {
public:
ParkingSystem(int big, int medium, int small) {
carPosition[0] = big;
carPosition[1] = medium;
carPosition[2] = small;
}
private:
int carPosition[3];
public:
bool addCar(int carType) {
return carPosition[carType - 1]-- > 0; // 判断停车位是否满和让停车位减一
}
};
/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/