https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PjMgaALgDFAUq
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.Scanner;
public class Solution {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int t = 1; t <= T; t++) {
int N = scanner.nextInt(); // Command의 수
int[][] command = new int[N][2];
for (int n = 0; n < N; n++) {
command[n][0] = scanner.nextInt(); // 0:현재 속도 유지, 1:가속, 2:감속
if (command[n][0] != 0)
command[n][1] = scanner.nextInt(); // 1(가속), 2(감속) -> 가속도 입력(1 or 2)
}
int speed = 0;
int totalDistance = 0;
for (int n = 0; n < N; n++) {
if (command[n][0] == 0) // 현재 속도 유지할 경우
totalDistance += speed;
else if (command[n][0] == 1) { // 가속할 경우
speed += command[n][1];
totalDistance += speed;
}
else if (command[n][0] == 2) { // 감속할 경우
if (speed - command[n][1] < 0)
speed = 0;
else {
speed -= command[n][1];
totalDistance += speed;
}
}
}
System.out.println("#" + t + " " + totalDistance);
}
scanner.close();
}
}
'Algorithm > 백준+프로그래머스+SWEA+정올+구름' 카테고리의 다른 글
[Algorithm] SWEA 3499 퍼펙트 셔플 (0) | 2021.08.07 |
---|---|
[Algorithm] SWEA 1861 정사각형 방 (0) | 2021.08.06 |
[Algorithm] SWEA 1225 암호생성기 (0) | 2021.08.05 |
[Algorithm] SWEA 1218 괄호짝짓기 (0) | 2021.08.05 |
[Algorithm] 백준 2493 탑 (0) | 2021.08.05 |