HDU 1008

深入理解数据库原理
以实战为基础,以数据库原理为基础,结合实战,深入理解数据库原理。
wangxiaoming

Elevator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36920    Accepted Submission(s): 20120


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
 

一道水题 ,贴出这个博客 ,主要是想大家知道 有记录当前值 和下一个值的意思 在动态规划里用的比较多;

算法思想:

               这里不多说

源代码:

#include<iostream>
using namespace std;

int main()
{
    int n ;
    int i;
    int nexte; //用来记录下个楼层
    while(cin>>n && n)
    {
        int curent = 0;  //当前楼层
        int sum = 0;
        int i = 0;
        while(i < n)
        {
            cin>>nexte;
            if(nexte > curent)
            {
                sum+= (nexte - curent)*6 + 5;
            }
            else
            {
                sum +=(curent - nexte)*4 + 5;
            }
            curent = nexte;
            i++;
        }
        cout<<sum<<endl;

    }
    return 0;
}



wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值