频道栏目
首页 > 资讯 > 其他 > 正文

PAT-A 之1008 Elevator (C++)

18-07-26        来源:[db:作者]  
收藏   我要投稿
题目描述

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);
}
相关TAG标签
上一篇:外观模式(Facade)解析
下一篇:Linux实现两台服务器之间传输文件和文件夹的方法讲解
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站