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 }
  • 相关阅读:
    思路不够清晰
    深思不够
    [Android学习笔记]理解焦点处理原理的相关记录
    移动端自动化测试(二)之 Appium常用的API(python)函数介绍
    移动端自动化测试(一)之 Appium+Pyhton环境准备篇
    如何修改上线网站
    sublime如何自动保存
    自动化元素定位
    OC 字典 存储联系人信息 求大神优化代码
    我的第一个字典-Dictionary
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/4054517.html
Copyright © 2011-2022 走看看