请编写代码实现上面的问题
时间: 2024-03-18 09:38:48 浏览: 30
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_N 200
#define MAX_M 1000
typedef struct {
int from; // 起点楼层
int to; // 终点楼层
} Request;
int main()
{
int n, u, d, s; // 电梯参数
Request reqs[MAX_M]; // 请求
int num_reqs; // 请求数量
int cur_floor; // 当前电梯所在楼层
int direction; // 电梯当前运行方向,1 表示向上,-1 表示向下,0 表示静止
int t; // 电梯运行的总时间
int i, j, k, case_num;
// 处理多组数据
case_num = 1;
while (scanf("%d %d %d %d", &n, &u, &d, &s) == 4) {
// 读入请求
num_reqs = 0;
char line[10];
while (fgets(line, sizeof(line), stdin) != NULL && line[0] != '\n') {
sscanf(line, "%d-%d", &reqs[num_reqs].from, &reqs[num_reqs].to);
num_reqs++;
}
// 初始化电梯状态
cur_floor = n;
direction = 0;
t = 0;
// 处理请求
for (i = 0; i < num_reqs; i++) {
// 计算电梯运行到请求所在楼层的时间
int time_to_req = abs(cur_floor - reqs[i].from) * (reqs[i].from > cur_floor ? u : d);
// 如果电梯当前静止,则更新方向
if (direction == 0) {
direction = (reqs[i].from > cur_floor) ? 1 : -1;
}
// 如果电梯走到请求所在楼层需要的时间大于等于之前的请求总时间,则需要等待
if (time_to_req >= t) {
t = time_to_req;
} else { // 否则,需要等待的时间是之前请求总时间与电梯到达请求所在楼层之间的差值
t += s + (t - time_to_req);
}
// 计算电梯运行到请求终点楼层的时间
int time_to_dest = abs(reqs[i].to - reqs[i].from) * u;
// 更新电梯状态
if (reqs[i].to > reqs[i].from) { // 请求向上
if (cur_floor <= reqs[i].from || direction == -1) { // 需要向上运行
t += (reqs[i].from - cur_floor) * u;
cur_floor = reqs[i].from;
t += s;
}
t += time_to_dest;
cur_floor = reqs[i].to;
direction = 1;
} else { // 请求向下
if (cur_floor >= reqs[i].from || direction == 1) { // 需要向下运行
t += (cur_floor - reqs[i].from) * d;
cur_floor = reqs[i].from;
t += s;
}
t += time_to_dest;
cur_floor = reqs[i].to;
direction = -1;
}
}
// 输出答案
printf("Case %d: %d\n", case_num++, t + cur_floor * s);
}
return 0;
}
```
阅读全文
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)