hdu 1008 Elevator

来源:互联网 发布:淘宝双十一超级红包 编辑:程序博客网 时间:2024/05/29 04:03

Elevator

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


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 23 2 3 10
 

Sample Output
1741
题目大意:
本题是一个上电梯的题,首先声明,电梯在0层停着,电梯每上升一层需要花费6秒,下降一层需要花费4秒,停留一次需要5秒,当达到要求以后电梯不用回到0层。想必已经理解题意了把。
题目解析:
这是一个最简单不过的模拟水题了,我们只要判断下一次减去上一次是上升了还是下降了就可以了,首先下一位减去前一位如果大于0证明上升了,否则下降了,这样代码就很容易写出来了。大家看一下下面的代码想必就很清楚了。
代码:
#include<iostream>#include<cstring>using namespace std;int main(){    int N,a[100],i,t,cnt;    memset(a,0,sizeof(a));    while(cin>>N&&N)    {        t=0,cnt=1;        for(i=0; i<N; i++)            cin>>a[i];        t+=a[0]*6;        for(i=0; i<N-1; i++)        {            cnt++;            if(a[i+1]-a[i]>0)                t+=(a[i+1]-a[i])*6;            else                t+=(a[i]-a[i+1])*4;        }        cout<<cnt*5+t<<endl;    }    return 0;}


 

0 0
原创粉丝点击