• PTA (Advanced Level) 1008 Elevator


    Elevator

      The highest building in our city has only one elevator. A request list is made up with Npositive 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 Npositive 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个数,代表电梯具体停留的楼层,电梯初始在0层,每向上一层会消耗6秒,每向下一层会消耗4秒,在需停留层会停留5秒,电梯最后不需要返回0层。

      模拟一下电梯的运行过程即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int n, t = 0, nowf = 0; //n为电梯需停留层数, t为已经消耗的时间, nowf是电梯当前所在的楼层
     4 int main()
     5 {
     6     scanf("%d", &n);    //输入电梯需停留层数
     7     while(n--){
     8         int floors;
     9         scanf("%d", &floors);   //输入下一个要停留的楼层
    10         while(nowf < floors){   //向上运行
    11             nowf++;
    12             t += 6;
    13         }
    14         while(nowf > floors){   //向下运行
    15             nowf--;
    16             t += 4;
    17         }
    18         t += 5; //停留
    19     }
    20     printf("%d
    ", t);
    21     return 0;
    22 }
  • 相关阅读:
    Eclipse添加Junit测试
    Java基础—JDK环境变量配置
    Java基础—常用类之String类
    Spring3+ibatis (SQL Server)+pager-taglib.tld查询分页的实现
    【solr专题之中的一个】Solr高速入门
    【翻译自mos文章】在12c中Create or Truncate Table时非常慢,等待事件为 DFS Lock Handle wait
    Ubuntu x86 64 settup nginx rtmp server
    [POJ 1390]Blocks
    Spring中AOP的使用
    Cocos2dx 小技巧(十三)聊聊坐标系
  • 原文地址:https://www.cnblogs.com/suvvm/p/10672202.html
Copyright © 2020-2023  润新知