很简单的电梯问题,上一层需要6秒,下一层需要5秒,到一层停留需要5秒。唯一需要注意的就是当目标楼层与当前楼层相同的时候,也需要停留5s,否则会有一些测试点无法通过。
#include <cstdio>
// 上楼需要5s
// 下楼需要4s
// 停留6s
int main()
{
int num;
scanf("%d", &num);
int * stopFloor = new int[num];
for (int i = 0; i < num; i++)
{
scanf("%d", &stopFloor[i]);
}
int currentFloor = 0;
int time = 0;
for (int i = 0; i < num; i++)
{
if (stopFloor[i] > currentFloor)
{
time += (stopFloor[i] - currentFloor) * 6 + 5;
currentFloor = stopFloor[i];
}
else if (stopFloor[i] < currentFloor)
{
time += (currentFloor - stopFloor[i]) * 4 + 5;
currentFloor = stopFloor[i];
}
else
{
time += 5;
}
}
printf("%d", time);
return 0;
}