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 Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
Output Specification:
For each test case, print the total time on a single line.
Sample Input:
3 2 3 1
Sample Output:
41
题目意思:电梯从0层开始向上运行,依次给出电梯所到达的楼层数,电梯上升一层需要6s,电梯下降一层需要4s,电梯停下来需要5s,问走完所有电梯要到达的楼层总共花了多少时间。
解题思路:确定电梯当前是向上还是向下,若是a[i]>a[i-1],向上运行,需要(a[i]-a[i-1])*6秒;若是a[i]<a[i-1],向下运行,需要(a[i]-a[i-1])*4秒。同时每停止一次需要5秒,最后累加起来即可。
#include<iostream> #include<algorithm> #include<string> #include<cstdio> using namespace std; int main() { int i,n,sum=0; int a[101]; cin>>n; a[0]=0; for(i=1;i<=n;i++) { cin>>a[i]; } for(i=1;i<=n;i++) { if(a[i]>=a[i-1]) { sum+=(a[i]-a[i-1])*6; } else { sum+=(a[i-1]-a[i])*4; } } sum+=n*5; cout<<sum; return 0; }