博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1008 Elevator
阅读量:4541 次
发布时间:2019-06-08

本文共 1417 字,大约阅读时间需要 4 分钟。

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 太简单了,竟然是按要求的顺序进行升降,一点都没有说进行时间优化?!
1 #include 
2 using namespace std; 3 4 int main() 5 { 6 int N; 7 cin >> N; 8 int pre = 0, now,sum = 5 * N;//上一次停留在哪?现在要去的楼层,时间初始为要在 每一层的总停留时间 9 for (int i = 0; i < N; ++i)10 {11 cin >> now;12 if ((now - pre) > 0)//上13 sum += (now - pre) * 6;14 else if ((now - pre) < 0)//下15 sum += (pre - now) * 4;16 pre = now;//更新楼层17 }18 cout << sum << endl;19 return 0;20 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11173076.html

你可能感兴趣的文章
Redis 禁用FLUSHALL FLUSHDB KEYS 命令
查看>>
Matlab中imread函数使用报错“不应为MATLAB 表达式”分析
查看>>
MFC ADO数据库操作
查看>>
图像质量评价-NQM和WPSNR
查看>>
面试准备——相关知识
查看>>
每日一字:悟
查看>>
CentOS7.6安装稳定版Nginx
查看>>
LeetCode 1002. Find Common Characters (查找常用字符)
查看>>
建立隐藏管理员用户
查看>>
android设置图文提醒功能
查看>>
ajax跨域提交
查看>>
完成登录与注册页面的前端
查看>>
Mac下source tree 下的安装
查看>>
Q学习原理及例子
查看>>
rpmbuild 源码打包clickhouse,附带打好的rpm包下载地址
查看>>
软件体系结构原理、方法与实践总结
查看>>
2017-2018-1 《程序设计与数据结构》第3周学习总结
查看>>
一些基础语法
查看>>
360多万条信息把一台服务器快拖卡了
查看>>
Git详解之六 Git工具
查看>>