题目

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. 设置虚拟机静态ip

    1.查看本机ip.网关.dns服务器 IPv4地址:本机局域网ip 路由器:网关地址 nameserver:局域网内部dns服务器 其他dns服务器 移动.电信和联通:114.114.114.114 ...

  2. .NET via C#笔记5——基元类型,引用类型和值类型

    5 基元类型,引用类型和值类型 5.3 值类型的装箱和拆箱 将值类型转化为引用类型需要进行装箱(boxing) 赋值,传参等操作,如果从值类型转为引用类型,都会进行装箱 装箱的代价比较大 申请一块堆内 ...

  3. Android_03android拨号软件

    今日开发了一个简单的安卓拨号软件. 步骤如下: 1.先构建用户界面 2.写java代码获取控件 3.进行逻辑的控制 4.添加权限 1.构建用户界面 2.点进R.java文件会发现系统已经自动生成索引, ...

  4. UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)

    题意:平面上有n(n <= 1000)个点,每个点为白点或者黑点.现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大.隔板上的点可以看做是在任意一侧. 分析:枚举每个基准点i,将 ...

  5. AD中内电层设置

    用于走线与普铜 内电层分割

  6. Day3-T4

    原题目 Describe:有点恶心的DP+最短路 code: #include<bits/stdc++.h> using namespace std; long long A,B,C,z, ...

  7. 安装kubernetes遇见coredns坑

    安装kubernetes遇见问题 kubectl describe pod coredns -n kube-system, 查看发现coredns readiness 一直unhealthy, 并且一 ...

  8. gogs 小团队使用 2

    gogs 团队使用第二种方法如下, 前面办法参考前面的方法: 由 root 用户新建 organization, 比如说建立 hardware,然后把团队的 技术负责人拉到 owners 这个 tea ...

  9. MongoDB四-操作索引

    转自: http://www.cnblogs.com/huangxincheng/archive/2012/02/29/2372699.html 我们首先插入10w数据,上图说话: 一:性能分析函数( ...

  10. springboot~不用模板执行html文件

    放到在resources/static目录下,创建目录或html文件,均可.如: