출처: SW Expert Academy
제출 날짜: 2021년 8월 8일 일요일
상태가 0이라면 직전 속도와 같고, 상태가 1이라면 (직전 속도+입력 받은 속도)가 된다. 상태가 2라면 (직전 속도-입력 받은 속도)지만 해당 값이 0보다 작다면 0이 된다.
처음에 상태와 속도를 입력 받을 때, 상태가 2(감속 상태)라면 속도도 0부터 시작한다는 걸 처음에 생각을 못해서 시간이 좀 걸렸다.
// 출처: SW Expert Academy 1940번 가랏! RC카!
import java.util.Scanner;
import java.io.FileInputStream;
class SWEA1940 {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
int N = sc.nextInt(); // Command 의 수 (2 ≤ N ≤ 30)
int state = sc.nextInt(); // 첫번째 상태. 0:현재 속도 유지, 1:가속, 2:감속
int speed = 0; // 첫번째 속도
if (state == 1) {
speed = sc.nextInt();
} else if (state == 2) { // 시작부터 감소라면 속도는 0이다
speed = sc.nextInt();
speed = 0;
}
int total = speed; // 답. N 초 동안 이동한 거리
for (int i = 1; i < N; i++) { // 두번째부터 N번째까지
state = sc.nextInt(); // 상태
if (state == 0) { // 현재 속도 유지라면 이전 속도를 더한다
} else if (state == 1) { // 가속이라면 속도는 이전+현재
int s = sc.nextInt();
speed = s + speed;
} else if (state == 2) { // 감속이라면 속도는 이전-현재
int s = sc.nextInt();
speed = speed - s;
if (speed <= 0) { // 현재 속도보다 감속할 속도가 더 클 경우, 속도는 0 m/s
speed = 0;
}
}
total = total + speed;
}
System.out.println("#" + test_case + " " + total); // 답 출력
}
}
}
'알고리즘 > SWEA' 카테고리의 다른 글
SWEA 1974번 스도쿠 검증 (0) | 2021.09.06 |
---|---|
SWEA 11315번 오목 판정 (0) | 2021.08.19 |
SWEA 1228번 암호문1 (0) | 2021.08.09 |