|
Problem Description
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
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
|
|
Output
Print the total time on a single line for each test case.
|
|
Sample Input
1 2 3 2 3 1 0 |
|
Sample Output
17 41 |
#include "stdio.h"
int main(){
int a,time=0;
while(scanf("%d",&a)!=EOF&&a!=0){
int b[a+1]; //GCC可以编译通过 C不行,数组定义存在问题
b[0]=a;
while(a){
scanf("%d",&b[a]);
a--;
}
a=b[0];
b[0]=0; //从题目中得:电梯初始位置是0层
time=b[a]*6+5;
for(;a>1;a--){
if(b[a]<b[a-1]){
time=time+(b[a-1]-b[a])*6+5;
}
else{
time=time+(b[a]-b[a-1])*4+5;
}
}
printf("%d\n",time);
}
return 0;
}
如果有什么不正确或不合适的地方,希望大家能在评论里面指出
本文介绍了一个基于特定电梯移动规则的时间计算问题。电梯在不同楼层间移动以完成请求,需计算完成所有请求所需的总时间。文章提供了完整的代码实现,并考虑了电梯上行与下行的不同耗时。
1048

被折叠的 条评论
为什么被折叠?



