题目

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" cont ...

  2. 类的始祖Object

    一.概述 Object时java中顶级父类,也是唯一没有父类的类:它是整个java中最基本的类,在java中所有的类都默认继承了Object. 二.重要方法 1.clone方法 克隆出一个新的对象. ...

  3. SpringBoot+SpringSecurity之如何forword到登录页面

    当我们在项目中引入了SpringSecurity框架进行身份校验的时候,如果某个请求需要用户身份认证,那么SpringSecurity会将用户redirect到登录页面.但是有些时候我们希望是forw ...

  4. c++ 配置ffmpeg

    本教程只针对windows64/32+vs2013环境配置第一步 :配环境1.打开ffmpeg官网中编译好的windows版本http://ffmpeg.zeranoe.com/builds/64位w ...

  5. web.xml中filter加载顺序出现的问题

    刚刚遇到了一个问题,项目中需要用到characterEncodingFilter和HiddenHttpMethodFilter,但是post请求还是会中文乱码,找了半天原因,后来发现,filter加载 ...

  6. 如何写好一个完整的Essay写作论证

    主体段是我们留学生在Essay写作中陈述观点和论述观点的核心段落,那么一个完整的论证应该包含哪些要素呢?我觉得有这么几项:主旨句.解释.例证.小结(非必需) 这些其实也是我们在说服他人接受我们的观点时 ...

  7. stm32f4 dma + uart idle + double 调试小记

    使用 stm32f4 调试uart 接收, 使用 空闲中断,dma 双缓冲模式,有以下几点需要注意的. 调试的时候断点不要打在 if (USART_GetITStatus(USART6, USART_ ...

  8. 【数据结构】C++语言环形队列的实现

    队列--先进先出 队列的一个缺点--出队后的内存空间浪费了,不能二次利用 环形队列--解决以上缺点的队列,用过的内存空间可以重复利用 github: https://github.com/HITFis ...

  9. C++命名规范——谷歌规范

    1.文件命名规则 文件名全部小写,可以含下划线或连字符,按项目约定命名,且尽量保证文件名明确.比如: cmd_save_player_info_class.cc ,my_use_full_class. ...

  10. 《打造扛得住的MySQL数据库架构》第7章 SQL查询优化

    SQL查询优化 7-1 获取有性能问题SQL的三种方法 如何设计最优的数据库表结构 如何建立最好的索引 如何拓展数据库的查询 查询优化,索引优化,库表结构优化 如何获取有性能问题的SQL 1.通过测试 ...