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;
    }
    
  • 相关阅读:
    JS中数组的排序
    JS中输入身份证号码,subString截取出生日,并判断性别
    JS中for循环输出三角形
    JS中for循环实现s=x^y。
    JS中用for实现n的阶乘
    JS实现:for循环输出1000以内水仙花数
    JS中用if..else 查询成绩
    JS——do...while循环输出斐波拉契数前20项
    JS中while循环 ,二分法,产生随机数,计算机猜几次能猜中
    2018年5月9日JAVA-servlet01
  • 原文地址:https://www.cnblogs.com/FrankOu/p/15037110.html
Copyright © 2011-2022 走看看