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
思路: 定义两个变量to和now,to表示下一楼层,now表示当前楼层,每次上楼用(to-now)*6累加时间,下楼用(now-to)*4累加时间,不要忘了还要加上停留的时间,哪怕从5楼到5楼没有上下楼也要加上时间5秒钟。遍历所有楼层之后输出最后的累加结果即可。
#include <iostream>
using namespace std;
int main()
{
int n, to, sum=0,now=0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &to);
if (to > now) sum += ((to - now) * 6);
else sum += ((now - to) * 4);
sum += 5;
now=to;
}
printf("%d", sum);
return 0;
}
复习:
这道题本身没有什么难度,关键是把题目意思弄清楚,给出的数据的第一个数表示的是数据的总数,而不是楼层编号,这个数字后面的才是楼层编号。