zoukankan      html  css  js  c++  java
  • The Legend of the Elevator(20210720练习赛D题题解)

    传送门

    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

    There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

    Output

    Print the total time on a single line for each test case.

    Sample Input

    1 2
    3 2 3 1
    0
    

    Sample Output

    17
    41
    

    题目分析

    一个电梯,上楼花6秒,下楼花4秒,到一层还得停5秒,给你给你多组数据,让你按照指定顺序到达不同的楼层,总共需要花多长时间?

    这道题就是一个纯模拟题,比较这次操作和前次操作上楼还是下楼就行了。

    写的时候最好把变量放到全局,且从下标1开始存,这样写会容易很多具体看下面的代码

    AC代码

    #include <bits/stdc++.h>
    #define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    #define rT printf("
    Time used = %.3lf
    ", (double)clock()/CLOCKS_PER_SEC)
    
    using namespace std;
    
    const int N = 1010;
    int a[N], n;
    int main() {
        io;   
        while (cin >> n && n) {
            for (int i = 1; i <= n; i++) cin >> a[i];
            int res = 0;
            for (int i = 1; i <= n; i++) {
                if (a[i] > a[i - 1]) {
                    res += (a[i] - a[i - 1]) * 6;
                } else {
                    res += (a[i - 1] - a[i]) * 4;
                }
                res += 5;
            }
            
            cout << res << '
    ';
        }
        
        return 0;
    }
    
  • 相关阅读:
    微服务下的持续集成-Jenkins自动化部署GitHub项目
    JDK新特性-Lambda表达式的神操作
    ActiveMQ详细入门教程系列(一)
    程序员必须了解的知识点——你搞懂mysql索引机制了吗?
    面试之后,扼腕叹息。
    哎,这让人抠脑壳的 LFU。
    延迟消息的五种实现方案
    Java实现Kafka生产者和消费者的示例
    Pytorch训练时显存分配过程探究
    Python命令行参数定义及注意事项
  • 原文地址:https://www.cnblogs.com/FrankOu/p/15037110.html
Copyright © 2011-2022 走看看