题目分析——
顾名思义,先将进行键盘输入各数组并存入数组;在计算过程中,对于第一个楼层我们必定是上升的(因为默认为第0层开始),同时到达第一个楼层时还会逗留5秒,故赋sum(总时间)的初值为a[1]+5;在接下来的各数组数据中,我们必须进行判断是上升还是下降,故应当引入Boolean类型判断;最后通过判断进行求和即可.
Problem Description
|
Input
|
Output
|
Sample Input 1 2 3 2 3 1 0 |
Sample Output 17 41 |
import java.util.Scanner;
public class Main1008 {
public static void main(String args[]) {
Scanner k = new Scanner(System.in);
int[] a = new int[100];
boolean[] b = new boolean[100];
int N, m, sum;
while (k.hasNextInt()) {
sum = 0;
N = k.nextInt();
if (N == 0) {
break;
}
else {
for (int i = 1; i <= N; i++) {
m = k.nextInt();
a[i] = m;
}
for (int i = 1; i < N; i++) {
if (a[i] < a[i + 1]) {
b[i] = true;
}
else {
b[i] = false;
}
}
sum = a[1] * 6 + 5;
for (int i = 1; i < N; i++) {
if (b[i] == true) {
sum = sum + (a[i + 1] - a[i]) * 6 + 5;
} else {
sum = sum + (a[i] - a[i + 1]) * 4 + 5;
}
}
System.out.println(sum);
}
}
}
}