• PAT 1008 Elevator 数学


    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 Specification:

    Each input file contains one test case. Each case contains a positive integer N, followed by N positive 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

    题目意思:电梯从0层开始向上运行,依次给出电梯所到达的楼层数,电梯上升一层需要6s,电梯下降一层需要4s,电梯停下来需要5s,问走完所有电梯要到达的楼层总共花了多少时间。

    解题思路:确定电梯当前是向上还是向下,若是a[i]>a[i-1],向上运行,需要(a[i]-a[i-1])*6秒;若是a[i]<a[i-1],向下运行,需要(a[i]-a[i-1])*4秒。同时每停止一次需要5秒,最后累加起来即可。

    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<cstdio>
    using namespace std;
    int main()
    {
        int i,n,sum=0;
        int a[101];
        cin>>n;
        a[0]=0;
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        for(i=1;i<=n;i++)
        {
            if(a[i]>=a[i-1])
            {
                sum+=(a[i]-a[i-1])*6;
            }
            else
            {
                sum+=(a[i-1]-a[i])*4;
            }
        }
        sum+=n*5;
        cout<<sum;
        return 0;
    }
  • 相关阅读:
    mapreduce框架详解【转载】
    Hadoop的基本命令【转载】
    mininet实验 设置带宽之简单性能测试
    Opendarlight Carbon 安装
    mininet实验 测量路径损耗率
    Controller与Switch建立连接
    OpenFlow协议
    Controller控制器
    进击的SDN
    SDN前瞻 传统网络的缺陷
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/11383520.html
Copyright © 2020-2023  润新知