• 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 }
  • 相关阅读:
    ASP.NET中JS简单调用webservices的方法
    WINDOWS SERVER 2003使用IIS服务配置WEB站点(转)
    Tomcat安装配置(转)
    javascript 调用.net后台的数据 和方法
    win7下,sql2005安装,提示 iis功能和com+目录要求 的解决
    Vista或windows7远程桌面控制服务器较慢 反应迟钝
    Asp.net Ajax的使用
    记录远程登陆用户的IP
    Ext.menu.Menu 属性及基础应用
    关于VS2005 无法使用切换到设计视图的解决方法
  • 原文地址:https://www.cnblogs.com/suvvm/p/10672202.html
Copyright © 2020-2023  润新知