【每天一道PAT】1008 Elevator
思路
每层存入数组;
对每层与前一层的层数进行比较;
每次比较后更新总时间;
#include <cstdio>
int floors[105];
int main()
{
int N;//层数
int time =0;//总时间
scanf("%d",&N);
for (int i = 0; i < N; ++i)
{
scanf("%d", &floors[i]);//数组存入每次层数
}
time =floors[0]*6+N*5;
for (int j = 1; j < N; ++j)
{
if(floors[j]>floors[j-1]) time+=6*(floors[j]-floors[j-1]);//判断为上楼
else if(floors[j]<floors[j-1]) time+=4*(floors[j-1]-floors[j]);//判断为下楼
}
printf("%d",time);
}