题目

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

题目分析

已知要到达的N个楼层数,初始楼层为0,上一层楼需6s,下一层楼需4s,每一层楼停靠5s,计算总耗时

解题思路

依次遍历楼层号,停靠时间可统一计算N*5s,也可以在每层楼进行累加

  • 若当前楼层号>上个楼层号,上楼,总耗时+=6s。每层楼停靠5s;
  • 若当前楼层号<上个楼层号,下楼,总耗时+=4s。每层楼停靠5s;

思路01(最优)

指针记录上个楼层的楼层号

思路02

数组记录每个楼层的楼层号

Code

Code 01

#include <iostream>
using namespace std;
int main(int argc,char * argv[]) {
int n;
scanf("%d",&n);
int t=n*5;
int cf,pf = 0;//cf 当前楼层,pf起始楼层0
for(int i=1; i<=n; i++) {
scanf("%d",&cf);
if(cf>pf) t+=(cf-pf)*6;
else t+=(pf-cf)*4;
pf = cf;
}
printf("%d",t);
return 0;
}

Code 02

#include <iostream>
using namespace std;
int main(int argc,char * argv[]) {
int n;
scanf("%d",&n);
int f[n+1]= {0}; //下标0处为哨兵,起始楼层0
int t=n*5;
for(int i=1; i<=n; i++) {
scanf("%d",&f[i]);
if(f[i]>f[i-1]) t+=(f[i]-f[i-1])*6;
else t+=(f[i-1]-f[i])*4;
}
printf("%d",t);
return 0;
}

PAT Advanced 1008 Elevator (20) [数学问题-简单数学]的更多相关文章

  1. PAT Advanced 1008 Elevator (20 分)

    The highest building in our city has only one elevator. A request list is made up with N positive nu ...

  2. PAT 甲级 1008 Elevator (20)(代码)

    1008 Elevator (20)(20 分) The highest building in our city has only one elevator. A request list is m ...

  3. PAT甲 1008. Elevator (20) 2016-09-09 23:00 22人阅读 评论(0) 收藏

    1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest ...

  4. PAT 甲级 1008 Elevator (20)(20 分)模拟水题

    题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...

  5. PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...

  6. PAT 1008. Elevator (20)

    1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest ...

  7. 1008 Elevator (20分)

    1008 Elevator (20分) 题目: The highest building in our city has only one elevator. A request list is ma ...

  8. 【PAT甲级】1008 Elevator (20分)

    1008 Elevator 题目: The highest building in our city has only one elevator. A request list is made up ...

  9. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

随机推荐

  1. 十八、JavaScript之布尔类型

    一.代码如下 二.运行效果 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...

  2. 1. Centos 安装

    安装 Centos 6.9 配置网络 vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes ...

  3. [题解] LuoguP4827 [国家集训队] Crash 的文明世界

    传送门 这个题......我谔谔 首先可以考虑换根\(dp\),但到后来发现二项式定理展开过后需要维护\(k\)个值,同时每个值也要\(O(k)\)的时间按二项式定理算 当然fft优化过后就是k lo ...

  4. webpack随笔2--编译ES6/ES7

    一.Babel 1.安装babel Bable-loader: babeljs.io babel最新版:npm install babel-loader@8.0.0-beta.0 @babel/cor ...

  5. Codeforces Round #616 (Div. 2)

    地址:http://codeforces.com/contest/1291 A题就不写解析了,就是给一个数,是不是本身满足这个条件或者删除某些数字来达到这个条件:奇数,各个位上的数字加起来是偶数. # ...

  6. 【Vue中的坑】Vue中的@mouseenter没反应?

    在开发中想实现鼠标悬浮,然后发现事件不由被出发,查找资料,发现并不是所有情况都不能用 下面就简单的说一下如何避免这种情况 如果你的悬浮事件是在 a 标签上,那么你直接使用就会出问题,你需要加一个nat ...

  7. 屏幕切换 onStart() onStop() onRestart() onDestroy()

    android:configChanges="orientation|keyboardHidden|screenSize"          //xml文件<activity ...

  8. JVM探秘:jinfo查看JVM运行时参数

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 如何查看JVM运行时参数,对于线上JVM调优是很关键的,因为只有知道了当前使用的JVM ...

  9. spring+springMVC+mybatis , 项目启动遇坑

    github上找的框架组合例子 结合自己的数据库作为新项目开发. 但是项目启动时,tomcat启动失败: 检查不出错误. 于是改换maven引入jetty插件来启动项目, 结果在未改动的任何代码的情况 ...

  10. 第二阶段scrum-2

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 正在连接配置数据库部分