题目

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;
}

最新文章

  1. Testlink安装问题收录
  2. Python基础篇【第3篇】: Python正则表达式
  3. C#深入浅出 C#语法中的重中之重——委托(四)
  4. CodeForces 622 A.Infinite Sequence
  5. Access获取新插入数据的自增长主键Id
  6. js隔行变色
  7. 打开新窗口(window.open)
  8. Maven之(七)pom.xml配置文件详解
  9. Java豆瓣电影爬虫——模拟登录的前世今生与验证码的爱恨情仇
  10. IDS 源镜像端口添加
  11. java构造函数和初始化
  12. 《Orange‘s》 Bochs环境配置
  13. Silverlight网页打开后马上崩溃,“白屏”,而且毫无提示
  14. Mysql 用命令行导出导入数据方法
  15. Git的一些资源链接
  16. ImageView和onTouchListener实现,点击查看图片细节
  17. create table repo_folder_operate_log_bak as select * from repo_folder_operate_log;
  18. C# DateTime 月第一天和最后一天 取法
  19. Mysql-表关系
  20. 1101: [POI2007]Zap

热门文章

  1. 【转】ASP.NET Core MVC/WebAPi 模型绑定探索
  2. 留学Essay写作:从入门到精通
  3. MySQL5.7忘记密码解决方案
  4. 08 SSM整合案例(企业权限管理系统):08.权限控制
  5. 第十四篇Django-model进阶(中介模型,查询优化,extra,整体插入)
  6. vue学习(二)Vue常用指令
  7. B. The Number of Products(Codeforces Round #585 (Div. 2))
  8. 关于博主 5ab
  9. 编程入门-Eclipse的断点调试
  10. 如何拖拽DIV边线并左右自适应改变大小?