본문으로 바로가기

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();
	}
}