1008 Elevator (20分)
题目
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
Output Specification:
For each test case, print the total time on a single line.
Sample Input:
3 2 3 1
Sample Output:
41
题目大意
在一个城市里,最高的建筑就是一个电梯。题目的要求是要给你一串数字,表示电梯中的人将要去的层数,你需要计算总共需要的时间,电梯上升一层的时间为6s,下降一层的时间是4s,在一层停留的时间为5s。输入首先为一个数字N,表示有数字串中总共有多少个数字,每个数字不超过一百,输出为所需时间。
题目分析
根据每一层需要的时间,不同情况进行讨论,最后得出结果。
代码
#include "stdio.h"
int main(){
int N;
int i;
int a[1005];
int thisfloor;
int sum;
scanf("%d",&N);//总共有几个数字
for (i = 0;i < N;i++){
scanf("%d",&a[i]);//将数字串存储在int型的数组中,计算时,每次取一个。
}
thisfloor = 0;
sum = 0;
for (i = 0;i < N;i++){//根据不同情况进行计算
if (a[i] - thisfloor > 0) sum += 6*(a[i] - thisfloor) + 5;//上楼时间为6*层数+5
if (a[i] - thisfloor == 0) sum += 5;//如果相同楼层,则加上停留时间5s
if (a[i] - thisfloor < 0) sum += 4*(thisfloor - a[i]) + 5;//下楼时间为4*层数+5
thisfloor = a[i];//计算结束后更新楼层
}
printf("%d",sum);
}