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专区查资料。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值