本文主要是介绍[ PAT-A ] 1008 Elevator (C++),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述 |
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
解题思路 |
一道水题,但是题目描述不严谨.
- 没明确抵达最后一层时,是否需要计算停留时间.做题者需从例子中才能解决疑问
- 如果出现重复楼层,是否需要另做考虑.根据WA之后的状况可知,此问题不需要考虑,即当出现重复楼层的时候,电梯仍然需要再次到达并停留
在以上两点疑问得到解决之后,题目就变得更水了~~
代码设计 |
//主要算法
//zhicheng
while(scanf("%d",&n)!=EOF)
{long long sum=0;int tmp1=0,tmp2;while(n--){scanf("%d",&tmp2);if(tmp2>tmp1)sum+=(tmp2-tmp1)*6;else sum+=(tmp1-tmp2)*4;tmp1=tmp2;sum+=5;}printf("%lld\n",sum);
}
有关PAT-B的更多内容可以关注 ——> PAT-B题解
铺子日常更新,如有错误请指正
传送门:代码链接 题目链接 PAT-A题解
这篇关于[ PAT-A ] 1008 Elevator (C++)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!