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 }
  • 相关阅读:
    在Swift中定义属于自己的运算符
    计算型属性 vs 懒加载
    swift- mutating
    什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
    微信授权登录-微信公众号和PC端网站
    PHP实现购物车的思路和源码分析
    PHP实现图片的等比缩放和Logo水印功能示例
    PHP实现IP访问限制及提交次数的方法详解
    Laravel 队列发送邮件
    laravel 定时任务通过队列发送邮件
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/4054517.html
Copyright © 2011-2022 走看看