zoukankan      html  css  js  c++  java
  • Elevator

    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

    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

     1 #include <stdio.h> 
     2 
     3 int main(){
     4     int number;
     5     int floor;
     6     int position;
     7     int time;
     8     int temp;
     9     
    10     while(1){
    11         scanf("%d",&number);
    12 
    13         if(number==0)
    14             break;
    15 
    16         position=0;
    17         time=0;
    18         while(number--){
    19             scanf("%d",&floor);
    20 
    21             if(floor==position)  //如果请求的楼层为当前楼层,则不用移动,时间加上电梯停留的时间
    22                 time+=5;
    23 
    24             else if(floor>position){  //电梯要往上升,计算移动时间和等待时间,改变当前楼层
    25                 temp=(floor-position)*6+5;
    26                 time+=temp;
    27                 position=floor;
    28             }
    29 
    30             else{    //电梯要往下降,计算移动时间和等待时间,改变当前楼层
    31                 temp=(position-floor)*4+5;
    32                 time+=temp;
    33                 position=floor;
    34             }
    35         }
    36 
    37         printf("%d
    ",time);
    38     }
    39         
    40     return 0;
    41 }
  • 相关阅读:
    DG查看恢复进度
    dataguard主备延迟多长时间的查询方法
    DG动态性能视图详解
    Django之ORM的增删改查操作流程
    IPython
    render函数和redirect函数的区别+反向解析
    http状态码
    图的基本概念
    图的遍历
    vue之webpack打包工具的使用
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/4054517.html
Copyright © 2011-2022 走看看