Redian新闻
>
有人做IL term life insurance吗?
avatar
有人做IL term life insurance吗?# Money - 海外理财
h*o
1
书上完整答案在哪儿? 书上说 in the downloadable code attachment. 不知道指什
么。
另外,我自己写的和它很不一样。有些functions 我不清楚到底写在那个类合适。 例
如:
park(), unpark() findAvaiableSlots()写在 class vehicle, 还是 class
ParkingLot 还是 ParkingSlot 好。
avatar
q*e
2
想做个quote. 谢谢
avatar
c*p
3
/。。。。。。。。我正想问这个,你就问了。。。
avatar
p*2
5
我觉得书上给的solution也不匝地
avatar
h*o
6
谢谢 sunsnap. cc150 的 答案 挺绕的。
二爷有没有比较好的思路?
你用什么数据结构存parking spot?
目前我用一维数组: parking_spot = new array [capacity];

【在 p*****2 的大作中提到】
: 我觉得书上给的solution也不匝地
avatar
c*p
7
对我也觉得这个答案不怎么好。你post出来你的答案给我们看看呗!

【在 h**o 的大作中提到】
: 谢谢 sunsnap. cc150 的 答案 挺绕的。
: 二爷有没有比较好的思路?
: 你用什么数据结构存parking spot?
: 目前我用一维数组: parking_spot = new array [capacity];

avatar
h*o
9
int test_parking_lots(){
ParkingLots p(3,40,50);//floor, row, column
Car car1 = Car();
Motor motor1 = Motor();
Bus bus1 = Bus();
int car1_id, motor1_id, bus1_id;
bool res;
res = p.park(car1, car1_id);
res = p.park(motor1, motor1_id);
res = p.park(bus1, bus1_id);
res = p.unpark(car1, car1_id);
return 0;
}
typedef enum VehicleType_e{
MOTOR_VEHICLE_TYPE = 0,
CAR_VEHICLE_TYPE,
BUS_VEHICLE_TYPE,
MAX_VEHICLE_TYPE_SIZE
} VehicleType;
typedef enum SpotType_e{
INVALID_SPOT_TYPE = -1,
COMPACT_SPOT_TYPE = 0,
LARGE_SPOT_TYPE,
MAX_SPOT_TYPE_SIZE
} SpotType;
typedef enum Needed_Spot_Num_e{
MOTOR_SPOT_NUM = 1,
CAR_SPOT_NUM = 1,
BUS_SPOT_NUM = 5
} Needed_Spot_Num;
class Vehicle{
public:
Vehicle(int num_spots):num_of_spots(num_spots){}
virtual void fitted_spot_type() = 0;
int num_of_spots;//note member variable of base must be assigned at base
class, not subclass.
bool canPark(SpotType spot_type);//can the vehicle park at this spot_
type
vector spot_type_list; //supported spot types
};
bool Vehicle::canPark(SpotType spot_type){
vector::iterator itt;
for (itt = spot_type_list.begin(); itt != spot_type_list.end();++itt
){
if (spot_type == *itt) return true;
}
return false;
}
class Car: public Vehicle{
public:
Car():Vehicle(CAR_SPOT_NUM){fitted_spot_type();} //note how num_of_spots
is assigned;
void fitted_spot_type(){spot_type_list.push_back(LARGE_SPOT_TYPE);}
};
class Motor: public Vehicle{
public:
Motor():Vehicle(MOTOR_SPOT_NUM){fitted_spot_type();}
void fitted_spot_type(){spot_type_list.push_back(COMPACT_SPOT_TYPE);spot
_type_list.push_back(LARGE_SPOT_TYPE);}
};
class Bus: public Vehicle{
public:
Bus():Vehicle(BUS_SPOT_NUM){fitted_spot_type();}
void fitted_spot_type(){spot_type_list.push_back(LARGE_SPOT_TYPE);}
};
class ParkingSpot{
public:
SpotType type;
int floor;
int row;
int column;
bool is_avaiable;
};
class ParkingLots{
public:
ParkingLots(int floor_size0, int row_size0, int col_size0):
floor_size(floor_size0), row_size(row_size0), col_size(col_size0){init
();}
~ParkingLots(){delete [] parking_spot;}
//vector * parking_spot;
ParkingSpot* parking_spot;
bool park(Vehicle& v, int& id);
bool unpark(Vehicle& v, int id);
bool have_enough_spots(Vehicle& v, int id);
private:
int floor_size, col_size, row_size, capacity;
void init();
};
void ParkingLots::init(){
assert(floor_size>0 && row_size>0 && col_size>0);
capacity = floor_size * row_size * col_size;
//parking_spot = new vector [capacity];
parking_spot = new ParkingSpot[capacity];
ParkingSpot* a = NULL;
int capacity_of_one_floor = col_size * row_size;
for (int i = 0; i < capacity; i++){
a = parking_spot+i;

if (i < 20) a->type = COMPACT_SPOT_TYPE;//assume
else a->type = LARGE_SPOT_TYPE;
a->is_avaiable = true;
a->row = i/col_size;
a->column = i % col_size;
a->floor = i /capacity_of_one_floor;
}
}
bool ParkingLots::park(Vehicle& v, int& id){
ParkingSpot* a = NULL;
for (int i = 0; i < capacity; i++){
a = parking_spot+i;
if (a->is_avaiable && have_enough_spots(v, i)){
id = i;
for(int j = 0; j < v.num_of_spots; j++){
a->is_avaiable = false;
}
return true;
}
}
return false;
}
bool ParkingLots::unpark(Vehicle& v, int id){
int row = id/col_size;
int col = id % col_size;
int floor = id /(col_size * row_size);
assert (floor < floor_size && row < row_size && col< col_size);
ParkingSpot* a = NULL;
for(int j = 0; j < v.num_of_spots; j++){
a = parking_spot+id + j;
if (a->is_avaiable) return false;//no vehicle here at all.
a->is_avaiable = true;
}
return true;
}
//check whether vehicle v can park starting at spot parking_spot[id]
bool ParkingLots::have_enough_spots(Vehicle& v,int id){
ParkingSpot* a = NULL;
int row, floor;

bool res = false;
for (int i = 0; i < v.num_of_spots; i++){
a = parking_spot+id + i;
if (i == 0) {row = a->row;floor = a->floor;}
if (a->floor != floor || a->row != row) return false; //no space in
the same floor and row
if (!v.canPark(a->type)) return false;//spot is not available
}
return true;
}

【在 c********p 的大作中提到】
: 对我也觉得这个答案不怎么好。你post出来你的答案给我们看看呗!
avatar
h*o
11
parking lot 这道题想考我们什么?
chat server 这道题想考我们什么?

【在 h**o 的大作中提到】
: xiexie.是挺简单的。
avatar
h*p
12
mark
avatar
u*o
13
chat server 那题有sample code可以参考吗?
我还知道一个DESIGN ELEVATOR的。。
这三题应该算是DESIGN里的三家马车了吧,时不时出来恶心一下大家。。

【在 h**o 的大作中提到】
: parking lot 这道题想考我们什么?
: chat server 这道题想考我们什么?

avatar
h*o
14
+hash?, not sure poker card

【在 u*****o 的大作中提到】
: chat server 那题有sample code可以参考吗?
: 我还知道一个DESIGN ELEVATOR的。。
: 这三题应该算是DESIGN里的三家马车了吧,时不时出来恶心一下大家。。

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。