首页 > 代码库 > hdu-1008 Elevator

hdu-1008 Elevator

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1008

题目:

Elevator

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


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
 

题意概括:

有一个电梯,上一层需要6s,在当前楼层停留需要5s,下一层需要4s,现在电梯要从第0层出发,问按顺序送完一些人到达相应的楼层需要的时间(不需要返回0层)。

输入:输入一个数n,按顺序输入这N个人要到的楼层

解题思路:

纯模拟过程,遇到比上一层大的往上走,遇见小的往下走,每停一次需要5s。

AC代码:

# include <stdio.h>int main (){    int n,i,sum,sta,a[200];    while(scanf("%d",&n)!=EOF)    {        if(n==0)            break;        sum=0; sta=0;        for(i=0;i<n;i++)            scanf("%d",&a[i]);        sum=a[0]*6+5; sta=a[0];        for(i=1;i<n;i++)        {            if(a[i]>a[i-1])                sum+=(a[i]-sta)*6+5;            else if(a[i]<a[i-1])                sum+=(sta-a[i])*4+5;            else                sum+=5;            sta=a[i];        }        printf("%d\n",sum);    }    return 0;} 

 

hdu-1008 Elevator