HDU1008 Elevator(模拟)
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1008
题意
一个电梯按照指定的顺序进行停留,向上一层需要6s,向下一层需要4秒,每停留一层需要5秒,起始出发点是在0层。
思路
一道简单的水题,按照题意来即可,没啥难度。
AC代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
/**
* Created with IntelliJ IDEA.
*
* @author wanyu
* @Date: 2018-02-19
* @Time: 12:37
* To change this template use File | Settings | File Templates.
* @desc
*/
public class Main {
private static final int UP = 6;//上
private static final int DOWN = 4;//下
private static final int STAY = 5;//停
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
while (true) {
in.nextToken();
int n = (int) in.nval;
if (n == 0) break;
int[] list = new int[n];
for (int i = 0; i < n; i++) {
in.nextToken();
list[i] = (int) in.nval;
}
int now = 0;
int sum = 0;
for (int i : list) {
if (now < i) {
//向上
int temp = (i - now) * UP + STAY;
sum += temp;
} else if (now > i) {
//向下
int temp = (now - i) * DOWN + STAY;
sum += temp;
} else {
//同一层
sum += STAY;
}
now = i;
}
System.out.println(sum);
}
}
}