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.
我们城市最高的建筑只有一部电梯。请求列表由N个正数组成。数字表示电梯将按指定顺序停在哪个楼层。将电梯向上移动一层需要6秒,向下移动一层则需要4秒。电梯每次停靠时将停留5秒钟。
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.
对于给定的请求列表,您要计算完成列表上的请求所花费的总时间。电梯一开始在0层,当请求得到满足时,不必返回底层。
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.
每个输入文件包含一个测试用例。每种情况都包含一个正整数N,后面跟着N个正数。输入中的所有数字都小于100。
Output Specification:
For each test case, print the total time on a single line.
对于每个测试用例,将总时间打印在一行上。
Sample Input:
3 2 3 1
Sample Output:
41
这题很水,但是要注意第一个数n是给了多少个数,并不是要去第几层,然后如果按下了当前层的话,电梯也得停止5秒
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int current_floor = 0;
int sum = 0;
for(int i = 0 ; i < n;i++)
{
int floor = sc.nextInt();
if(floor > current_floor)
{
sum += (floor - current_floor)*6 + 5;
}
else if(floor < current_floor)
{
sum += (current_floor - floor)*4 + 5;
}
else
{
sum += 5;
}
current_floor = floor;
}
System.out.println(sum);
}
}