1008. Elevator (20)

来源:互联网 发布:网络摄像头安装图解 编辑:程序博客网 时间:2024/06/05 14:38

题目链接:http://www.patest.cn/contests/pat-a-practise/1008

题目:

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

分析:

1)每层楼停5s,上一层楼要6s,下一层楼要4s。
2)这里有个注意点是对于最后一个到达点,也是要停5s的,所以最后应该是加上n * 5,而不是(n - 1) * 5。
3)刚开始是从0层开始的,最后不必回到0层。

案例分析:

先输入3,然后是2层,3层,1层。

从0层到2层,需要12秒

从2层到3层,需要6秒,

从3层到1层,需要8秒,

因为是停了3次,每次停5s,所以再加上15s,

总共41s。


AC代码:

#include<iostream>using namespace std;int nums[100];int main(void){ int i,j,temp_num = 0; int n; while(scanf("%d",&n) != EOF){  for(i = 1; i <= n; i ++){   scanf("%d",&nums[i]);  }  nums[0] = 0;//这是0层,表明从0层开始  int sum = 0;  for(i = 1; i <= n; i ++){   temp_num = nums[i] - nums[i - 1];   if(temp_num > 0)sum += temp_num * 6;//如果上楼的情况   if(temp_num < 0)sum += temp_num * (-4);//如果下楼的情况  }  sum += n * 5;  printf("%d\n",sum); } return 0;}



——Apie陈小旭

0 0