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
题目要求
计算乘电梯的总时间,上升一层需要6s,下降一层需要4s,停电梯开门的时间是5秒
- 首先输入测试个数的总个数N,然后输入N个测试的数字
- 输出乘电梯需要花费总时间
题目解析
用两个变量比较,一个变量temp0记录上次乘电梯去几层,一个temp1记录当前去几层
- 如果要上楼,则相差层数*6s
- 下楼,相差层数*4s
- 停止,N*5s
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cin>>n;
int temp0,temp1;
cin>>temp1;
temp0=temp1;
sum=temp1*6;
for(int i=0;i<n-1;i++)
{
cin>>temp1;
if(temp1>temp0)
sum=sum+(temp1-temp0)*6;
else
sum=sum+(temp0-temp1)*4;
temp0=temp1;
}
int result=sum+n*5;
cout<<result;
return 0;
}