题目来源: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): 70908 Accepted Submission(s): 38941
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.
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
Author
ZHENG, Jianqiang
题目分析:电梯向上一层需要6秒,向下一层需要4秒,每层停5秒,问电梯按输入顺序运行,每组数据需要多少秒实现。
解题思路:根据输入输出数据可知,电梯默认从第0层开始运行,每组数据先输入一个数代表电梯共停几次,再进行计算;第一个数据乘以六,其他数据与前一个数据比较,若较大差值乘6,否则差值乘4,最后加上次数乘5。
代码实现:
#include<stdio.h>
int main (void)
{
int N,m,i,j;
int a[110];
while(scanf("%d",&N),N!=0)
{
for(i=0;i<N;i++)
scanf("%d",&a[i]);
m=a[0]*6;
for(i=0;i<N-1;i++)
{
for(j=1;j<N;j++)
{
if(i==j-1)
{
if(a[i]<a[j])
m+=(a[j]-a[i])*6;
else
m+=(a[i]-a[j])*4;
}
}
}
printf("%d\n",m+5*N);
}
return 0;
}