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.





如果toFloor < fromFloor,花费的时间为(fromFloor - toFloor)*4+5。
import java.io.PrintStream; import java.util.Scanner; public class Main { public static Scanner in = new Scanner(System.in); public static PrintStream out = System.out; public static void main(String[] args) { int n = in.nextInt(); int time = 0; int toFloor,fromFloor = 0; for(int i=0;i<n;++i){ toFloor = in.nextInt(); time += moveElevator(fromFloor,toFloor); fromFloor = toFloor; } out.println(time); } // 从fromFloor到toFloor花费的时间 private static int moveElevator(int fromFloor, int toFloor) { if(toFloor<fromFloor){ return (fromFloor-toFloor)*4+5; } return (toFloor-fromFloor)*6 + 5; } }