前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】Elevator

【PAT甲级】Elevator

作者头像
喜欢ctrl的cxk
发布2019-11-08 13:41:41
3330
发布2019-11-08 13:41:41
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/88782541

Problem Description:

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:

代码语言:javascript
复制
3 2 3 1

Sample Output:

代码语言:javascript
复制
41

解题思路:

这道水题在18年HBU校赛给的模拟题库里有 就是数值不一样而已,电梯上升一层需要6秒,电梯下降一层需要4秒,到达指定楼层需要停5秒。无脑用自定义函数getTime()来分段对每一次电梯运行进行求解就行了。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

int getTime(int start,int end)   //求start到end的运行时间
{
    int time = 0;
    if(start > end)   //电梯下降需要4秒一层
    {
        time += 4*(start-end);
    }
    else    //电梯上升需要6秒一层
    {
        time += 6*(end-start);
    }
    time += 5;    //到达指定楼层停5秒
    return time;
}

int main()
{
    int N;
    cin >> N;
    int a[N];
    int start = 0, end;    //start为当前楼层,end为目标楼层
    int time = 0;     //电梯总用时
    for(int i = 0; i < N; i++)
    {
        cin >> end;
        time += getTime(start,end);
        start = end;
    }
    cout << time << endl;
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档