PAT Advanced 1008 Elevator (20) [数学问题-简单数学]
题目
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) [数学问题-简单数学]的更多相关文章
- 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 ...
- PAT 甲级 1008 Elevator (20)(代码)
1008 Elevator (20)(20 分) The highest building in our city has only one elevator. A request list is m ...
- 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 ...
- PAT 甲级 1008 Elevator (20)(20 分)模拟水题
题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...
- PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642
PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...
- PAT 1008. Elevator (20)
1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest ...
- 1008 Elevator (20分)
1008 Elevator (20分) 题目: The highest building in our city has only one elevator. A request list is ma ...
- 【PAT甲级】1008 Elevator (20分)
1008 Elevator 题目: The highest building in our city has only one elevator. A request list is made up ...
- 【PAT Advanced Level】1008. Elevator (20)
没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...
随机推荐
- TensorFlow2 Part2:基础知识回顾
python面向对象编程回顾 基础概念: 面向对象的编程简称OOP,它把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向对象的设计思想是抽象出Class,根据Class(类)创建In ...
- 九、JavaScript之分号使用,支持一行多语句
一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...
- 十九、SAP查询所有数据库表的所有数据
一.我们打款SAP自带的一个演示数据库SCARR 二.表结构如下 三.代码如下 四.执行结果如下 我们对比一下数据库的内容,很perfect 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微 ...
- vue左侧菜单的实现
后端实现 django视图def menu(request): menu_list = models.Menu.objects.all().values('id', 'menu_name', 'par ...
- Maccms后门分析复现(并非官网的Maccms){10.15 第二十二天}
该复现参考网络中的文章,该漏洞复现仅仅是为了学习交流,严禁非法使用!!!! Maccms官网:http://www.maccms.cn/ Maccms网站基于PHP+MYSQL的系统,易用性.功能良好 ...
- SpringBoot安全认证Security
一.基本环境搭建 父pom依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactI ...
- 22 ~ express ~ 内容评论实现
1,使用 ajax 提交评论内容 给 api.js 2,数据库 contents 增加评论字段 3,后台路由 api.js 接收并完成存储 /** 增加评论(用户,内容,时间) */ router.p ...
- UltraISO制作manjaro系统盘,使用优盘
下载系统镜像 默认是清华镜像 刻入方式一定要选RAW方式
- 二分图匹配 最大匹配数+最大点覆盖 POJ 1469+POJ 3041
最大匹配数就等于最大点覆盖,因为在图里面,凡是要覆盖的点必定是连通的,而最大匹配之后,若还有点没有覆盖到,则必定有新的匹配,与最大匹配数矛盾,如果去掉一些匹配,则必定有点没有覆盖到. POJ 1469 ...
- Sqlserver 增删改查----增
注意我说的常见查询,可不是简单到一个表得增删改查,做过实际开发得人都知道,在实际开发中,真正牵扯到一个表得增删改查只能说占很小得一部分,大多都是好几个表的关联操作的. 下面我就说一下我在实际开发中经常 ...