题目描述:
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.
输入描述:
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个数为电梯停靠的层数)
输出描述:
For each test case, print the total time on a single line.
输入例子:
3 2 3 1
输出例子:
41
***代码实现:***(提交时要求主类名为Main,且不能有注释,这里请各位自行修改!)
import java.util.Scanner;
/*
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.
* */
public class Elevator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] strs = str.split(" ");
int before = 0;
int result = 0;
for (int i = 1;i<strs.length;i++){
//跳过第一个数
int now = Integer.parseInt(strs[i]);
if (now>before){
//上升每层6秒
result+=(now-before)*6;
//更改before
before = now;
}else if (now<before){
//下降每层4秒
result += (before-now)*4;
before = now;
}
result+=5;
}
System.out.println(result);
}
}