HDU 1008 elevator

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
 
 
题意:有一架电梯,上一层楼需要6秒,下一层楼需要4秒,到达指定楼层后停留5秒,假设每次开始时电梯都停留在第0层。输入一个正整数N,表示电梯要停留N次,输入每次要停留的楼层,输出电梯按照指定的顺序走完全程需要花费的时间。
分析:用数组存储要停留楼层的数据,用第i个数据与第i-1个数据相减,用来判断电梯是上楼还是下楼。要注意每次开始要将TimeSum置为0,计算中要累加。
AC源代码(C语言):
 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int str[101],TimeSum,i,dis,N;
 6     while(scanf("%d",&N)&&N!=0)
 7         {
 8             TimeSum=0;
 9             str[0]=0;                           /*数组第一个数置为0,表示第0层*/
10             for(i=1;i<=N;i++)
11                 {
12                     scanf("%d",&str[i]);
13                     dis=str[i]-str[i-1];
14                     if(dis>0)                   /*判断上下楼*/
15                         {
16                             TimeSum+=dis*6+5;   /*要用+=,否则无法完成累加*/
17                         }
18                     else
19                         {
20                             TimeSum+=-dis*4+5;
21                         }
22                 }
23             printf("%d\n",TimeSum);
24         }
25     return 0;
26 }

2013-04-11

原文地址:https://www.cnblogs.com/fjutacm/p/3014325.html