PAT 甲级 1008 Elevator (20 分)(Java)
题目
题意解读
数学题,只需要注意不需要回到0层即可;
解法
解法一
import java.util.Scanner;
public class Main_1008 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean dir;
int pre = 0;
int res = 0;
for (int i = 0; i < n; i++) {
int m = sc.nextInt();
dir = m > pre ? true : false;
if(dir){
res += (m-pre) * 6;
}else{
res += (pre-m) * 4;
}
res += 5;
pre = m;
}
System.out.println(res);
}
}

本文解析了PAT甲级1008Elevator题目,涉及一个电梯向上移动的问题,重点在于避免返回0层。提供了一种使用Java编写的解法,通过判断电梯移动方向计算到达每一层的代价。适合Java程序员学习算法实战。
&spm=1001.2101.3001.5002&articleId=120914040&d=1&t=3&u=2df13da71d27406480752324cb9caa2f)
6万+

被折叠的 条评论
为什么被折叠?



