新手买吉他买哪种牌子:轮渡(b.pas)

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/27 09:21:12
有一艘车轮渡往来与河的两岸,运送车辆。轮渡上最多可以装载n辆车,过河一次需要t分钟。只要河的两岸有车在等待,船就要开。即如果船当前所在的一岸有车,那么它将载车马上过河;如果这一岸没有车,但是对岸有车在等,它会空船开过去;如果两岸此时都没有车,它就“休息”。如果岸边等待的车超过n量,那么按照到达的顺序,先到的先上,超过的要等下一班。
给出两岸车到达的情况,问每一辆车到达对岸的时刻。
输入第一行是n、t和m,m表示车的数量。接下来的m行,每行第一个数表示这辆车到达的时刻,第二个字符串是left或right,表示哪一岸。0<n,t,m<=10000。船初始时在左岸。车上下船的时间假设为0。输入的时刻一定是严格递增的。
按输入顺序,给出每一辆车到达对岸的时刻。

程序如下,用delphi写的,如果你用的不是delphi,可以把2~4行去除。

program b;
{$APPTYPE CONSOLE}
uses
SysUtils;

const
maxnum = 10000;
type
TBoat = record
at_left: boolean;
cur_time: integer;
end;
TCar = record
at_left: boolean;
org_time, arrive_time, index: integer;
end;
TCarArray = array[0..maxnum] of TCar;
var
n,t,m,i,count,index: integer;
boat: TBoat;
cars: TCarArray;
q: array[boolean] of TCarArray;
head,tail: array[boolean] of integer;
f: text;
location: string;
begin
assign(f, 'input.txt');
reset(f);
readln(f, n, t, m);
if ((n < 1) or (n > maxnum) or (t < 1) or (t > maxnum) or (m < 1) or (m > maxnum))
then
halt(1);
for i := 0 to m-1
do
begin
readln(f, cars[i].org_time, location);
if (pos('left',location) <> 0)
then
cars[i].at_left := true
else
cars[i].at_left := false;
cars[i].arrive_time := -1;
cars[i].index := i;
end;
close(f);

boat.at_left := true;
boat.cur_time := 0;
head[true] := 0;
tail[true] := 0;
head[false] := 0;
tail[false] := 0;

for i := 0 to m-1
do
begin
q[cars[i].at_left][tail[cars[i].at_left]] := cars[i];
inc(tail[cars[i].at_left])
end;

while ((head[true] <> tail[true]) or (head[false] <> tail[false]))
do
if ((head[boat.at_left] <> tail[boat.at_left]) and (q[boat.at_left][head[boat.at_left]].org_time <= boat.cur_time))
then
begin
count := 0;
while ((count < n) and ((head[boat.at_left] <> tail[boat.at_left]) and (q[boat.at_left][head[boat.at_left]].org_time <= boat.cur_time)))
do
begin
index := q[boat.at_left][head[boat.at_left]].index;
cars[index].arrive_time := boat.cur_time + t;
inc(count);
inc(head[boat.at_left]);
end;
inc(boat.cur_time, t);
boat.at_left := not boat.at_left;
end
else if ((head[not boat.at_left] <> tail[not boat.at_left]) and (q[not boat.at_left][head[not boat.at_left]].org_time <= boat.cur_time))
then
begin
inc(boat.cur_time, t);
boat.at_left := not boat.at_left;
end
else
begin
if ((head[not boat.at_left] = tail[not boat.at_left]) or ((head[boat.at_left] <> tail[boat.at_left]) and (q[boat.at_left][head[boat.at_left]].org_time < q[not boat.at_left][head[not boat.at_left]].org_time)))
then
boat.cur_time := q[boat.at_left][head[boat.at_left]].org_time
else
boat.cur_time := q[not boat.at_left][head[not boat.at_left]].org_time;
end;

assign(f, 'output.txt');
rewrite(f);
for i := 0 to m-1
do
writeln(f, cars[i].arrive_time);
close(f);
end.