分析:只是按顺序读取并处理,不需要求解最优解。首先读入的N,指示了需要在各楼层停留的时间,即:N*TIME_OF_STOP;之后,楼层设定为0;依次读入之后的N个正数,并与原楼层比较,在原楼层之上则(Subtraction)*TIME_OF_UP;否则为(Subtraction)*TIME_OF_DOWN
算法:
1.读入N;if(N==0)gotoStep9
2.curPos=0;TotalTime = N*TIME_OF_STOP;
3.N==0? gotoStep : gotoStep4;
4.读入pos;if(pos>curPos)gotoStep 5; else gotoStep 6;
5.TotalTime +=(pos-curPos)* TIME_OF_UP;
6.TotalTime += (curPos-pos)*TIME_OF_DOWN;
7.--N;gotoStep3;
8.printf(TotalTime);
9.Exit
AC code:
#include <stdio.h>
#define TIME_OF_STOP 5
#define TIME_OF_UP 6
#define TIME_OF_DOWN 4
int main()
{
int N, curPos, pos, totalTime;
while(scanf("%d", &N), N){
totalTime = N*TIME_OF_STOP;
curPos = 0;
while(N--){
scanf("%d", &pos);
if(pos>curPos)
totalTime += (pos-curPos)*TIME_OF_UP;
else
totalTime += (curPos-pos)*TIME_OF_DOWN;
curPos = pos;
}
printf("%d\n", totalTime);
}
return 0;
}