PAT练习题(甲级) 1008 Elevator (20分)(Java实现)

PAT练习题 1008 Elevator (20分)

题干

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

题意

  • 电梯上下问题,上楼每层需要6秒,下楼每层需要4秒,在每层停留时间是5秒。
  • 求解输入文件的结果,也就是总时间。

解题思路

  • 输入的第一个数字是后面总共有几个层数要求,记为N。
  • 下面的N行是楼层的变化
  • 输出总时间
  • 只需要对上楼和下楼进行相应的运算即可

代码实现

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: 胡奇夫
 * @QQ: 1579328766
 * @CreateTime: 2020-05-11-22-20
 * @Descirption: Pat甲级1008
 */
public class Testeight {
    static int current = 0;//当前楼层
    static int nextfloor;//下一个要去的楼层
    static int sum;//总时间
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] list = br.readLine().split(" ");
        for(int i = 0;i<Integer.parseInt(list[0]);i++)
        {
            nextfloor = Integer.parseInt(list[i+1]);
            if(nextfloor>=current)
            {
                sum += (nextfloor-current)*6+5;
            }
            else {
                sum += (current-nextfloor)*4+5;
            }
            current = nextfloor;
        }
        System.out.println(sum);
    }

}

总结

  • 之前有人问我为什么不用Scanner,是因为Scanner有点慢,BufferedReader快,毕竟有缓冲区,相关知识可以到Java专区查资料。

猜你喜欢

转载自blog.csdn.net/weixin_45062103/article/details/106063689