题目:
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.
分析:
用数组m来记录层数。设置m[0]为0意为从0层开始。如果相比于前一个记录上升,则6秒一层;如果下降,则4秒一层,停留时间为5秒。
代码:
#include <iostream>
using namespace std;
int main() {
int n;
while (cin>>n) {
if (n==0) {
break;
}
int m[n];
int count=0;
m[0]=0;
for (int i=1; i<=n; i++) {
cin>>m[i];
}
for (int i=1; i<=n; i++) {
if (m[i]>m[i-1]) {
count += 6*(m[i]-m[i-1]);
count += 5;
}
else if(m[i]<m[i-1]) {
count += 4*(m[i-1]-m[i]);
count += 5;
}
else {
count += 5;
}
}
cout<<count<<endl;
}
return 0;
}