A1008 Elevator (20 point(s))

简单推理,电梯上加,下减

1. 原文

原题

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:

1
3 2 3 1

Sample output:

1
41

2. 解析

上行x6 下行x4 停5

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<cstdio>
int main()
{
int n;
scanf("%d",&n);
int a;
int sum=0; int idx=0; int sub=0;
for (int i = 0; i < n; ++i)
{
scanf("%d",&a);
sub=a-idx;
if (sub>0)
{
sum+=sub*6;
}else{
sub=-sub;
sum+=sub*4;
}
sum+=5;
idx=a;
}
printf("%d\n",sum);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1008/
  • 发布时间: 2019-09-03 12:36
  • 更新时间: 2021-10-29 13:58
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!