【PAT甲级 - C++题解】1008 Elevator

简介: 【PAT甲级 - C++题解】1008 Elevator

1008 Elevator


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


题意

给定一个列表,记录着电梯需要一次到达的楼层,已知电梯上升一层需要 6 秒,下降一层需要 4 秒,在每层停留的时间为 5 秒,从第 0 层开始,需要计算电梯总花费时间。


注意:电梯可能在一层停留多次,每停留一次都要加 5 秒。


思路

具体思路如下:


1.初始化 pre = 0 即电梯从第 0 层开始,且 cur 表示当前遍历到的楼层,以及 res 用来保存电梯总花费时间。

2.每输入一个 cur 都计算一次花费的时间,如果上升就用 6 乘以上升楼层, 下降就用 4 乘以下降楼层,最后再加上楼层停留的时间 5 秒。

3.输出计算结果 res 。


代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, pre = 0, cur, res = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> cur;
        if (cur > pre) res += 6 * (cur - pre);   //上升时间
        else if (cur < pre)    res += 4 * (pre - cur);   //下降时间
        res += 5; //停留时间
        pre = cur;
    }
    cout << res << endl;
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
303 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
382 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
363 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
331 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
257 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
304 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
511 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
758 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)