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 ...
随机推荐
- 十八、SAP中使用IF/ELSE判断语句,以及sy-subrc的用法
一.sy_subrc为上一条语句的执行结果,如果为0,则表示执行成功. 需要注意的是,IF ELSE语句,每一个关键字都需要带句号 二.执行效果如下 相关代码如下 *&------------ ...
- ZOJ 3791 An easy game DP+组合数
给定两个01序列,每次操作可以任意改变其中的m个数字 0变 1 1 变 0,正好要变化k次,问有多少种变法 dp模型为dp[i][j],表示进行到第i次变化,A,B序列有j个不同的 变法总和. 循环 ...
- case...when...和decode——oracle
1.decode函数: 用法:decode(条件,值1,翻译1,值2,翻译2,......,缺省值): 例子: ','失败','未知') from table t1;--改变字段的显示值 ,变量1,变 ...
- Day1-T4
原题目 Describe:注意是“两次及以上”而不是“两种及以上”!! code: #include<bits/stdc++.h> using namespace std; int k,m ...
- zabbix监控linux 以及监控mysql
Zabbix监控Linux主机设置方法 linux客户端 :59.128 安装了mysql 配置zabbix的yum源 rpm -ivh http://repo.zabbix.com/zabbix/2 ...
- 创建Oracle序列sequence
create sequence SEQ_ID minvalue 1 maxvalue 99999999 start with 1 increment by 1 nocache order; 建解发器代 ...
- 使用maven构建项目的注意事项
一.如果修改了pom.xml文件,就有点类似修改了项目的结构,在再次运行项目前,应该Mvaen >>Update project一下. 二.对于依赖一个系列的的包,如spring,我们应该 ...
- 欧拉回路--P2731 骑马修栅栏 Riding the Fences
实在懒得复制题干了 *传送 1.定义 *如果图G(有向图或者无向图)中所有边一次仅且一次行遍所有顶点的通路称作欧拉通路. *如果图G中所有边一次仅且一次行遍所有顶点的回路称作欧拉回路. *具有欧拉回路 ...
- POJ 1149 网络流 合并建图
这个题目我敲了一个简单的EK,这不是难点 难点在于建图,按题目的要求 每个猪圈和顾客都建点的话,那也太多了...我看了Edelweiss里面的缩点方法才建好的图,哎,惭愧啊 实际那些猪圈根本不需要单独 ...
- 第二届中国“AI+”创新创业大赛完美收官,京东云赛道硕果累累
聚焦南京产业发展核心诉求,京东云携手南京政府构建的"平台+生态+赋能"的产业体系,搭建产业创新云平台,以人工智能产业创新链要素补齐为核心,围绕"研.产.供.销.服&quo ...