zoukankan      html  css  js  c++  java
  • 【PAT甲级】1008 Elevator (20分)

    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
    

    注意:

    • 这句句子一开始做题没看懂。


      翻译:每个例子包含一个正整数N,后面跟着N个正整数。

      (第一个数是数字的个数,第二个数串才是输入样例)

    ① Each case contains a positive integer N, followed by N positive numbers.


    • 每到达一个目标层次,停留5秒。包括最后一个目标层数

      The elevator will stay for 5 seconds at each stop.


    • (在这题目中没有出现,只是突然想到的)


      Java中有方法length()可以直接得到数组的长度;

      在C++中,字符串可以用strlen()得到长度,其他类型的数组用sizeof(数组名)/sizeof(数组的任一元素)




    代码:

    #include<iostream>
    using namespace std;
    
    int main() {
        int n; //N numbers
        cin>>n;
    
        int time=0;//time time
        int curr=0;//current floor
        int number;
        for(int i=0; i<n; i++) {
            cin>>number;
    
            if(curr<number) { //go up
                time+=(number-curr)*6+5;
            } else if(curr>number) {//go down
                time+=(curr-number)*4+5;
            } else { //same floor
                time+=5;//stay
                continue;
            }
            curr=number;
    
        }
    
        cout<<time<<endl;
    
        return 0;
    }
    
    

    运行结果:

  • 相关阅读:
    关于meta便签详解
    移动端等分比显示导航状态
    css3单选 复选按钮--代码分享
    css-样式重构-代码分享
    代码分享h5-sessionStorage,提示app下载代码块
    微信浏览器打开 点击下载app 无需提示使用浏览器打开--代码分享
    js 判断IOS版本号
    二进制,八进制,十进制,十六进制之间的转换
    JS组件系列——Bootstrap文件上传组件:bootstrap fileinput
    Bootstrap文件上传插件File Input的使用
  • 原文地址:https://www.cnblogs.com/musecho/p/12056363.html
Copyright © 2011-2022 走看看