pat甲级 1008 Elevator

题目:

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.

翻译一下:给一个电梯停放序列  初始电梯停在0楼,并且停在最后一层不用回到0楼   

上一楼需要6s,下一楼需要4s,电梯会在每一层停留5s

 

输入格式:(第一个数表示会去几个楼层,注意点:对于 3 3 3 3,虽然电梯后续没有上下层动,但是每一层还是得算停留的时间,time得加5

3 2 3 1

输出格式:

41

 

import java.util.Scanner;

public class pat1008 {
    public static void main(String[] args)  {
        Scanner scanner = new Scanner(System.in);
        String []a = scanner.nextLine().split(" ");//3 2 3 1
        int time=0;//时间花销
        int base = 0;//上次落在的楼层

        for(int i=1;i<a.length;i++){
            if(Integer.parseInt(a[i])>base){
                time = time + (Integer.parseInt(a[i])-base)*6;
                time = time+5;
                base = Integer.parseInt(a[i]);
                continue;
            }
            else if(Integer.parseInt(a[i])<base){
                time = time +(base-Integer.parseInt(a[i]))*4;
                time = time+5;
                base = Integer.parseInt(a[i]);
                continue;
            }
            else{
                time = time+ 5;
            }
        }

        System.out.print(time);
    }
}

 

posted @ 2021-04-05 19:47  chenyuan#  阅读(36)  评论(0编辑  收藏  举报