zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    memcached 在windows下安装及启动
    细说 ASP.NET Cache 及其高级用法
    asp.net MVC helper 和自定义函数@functions小结
    log4net 总结
    紧跟时代步伐,让我们拥抱MVC 3
    关于node-sass安装失败的解决办法
    table自适应
    获取select选中的值
    省市三级联动
    git bush 代码提交
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/11383520.html
Copyright © 2011-2022 走看看