-
1940. 가랏! RC카!알고리즘/SW Expert Academy 2022. 2. 24. 17:14728x90
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PjMgaALgDFAUq
문제 해석
RC카의 속도를 증가, 감소, 유지 시키며 N초동안 이동한 거리를 계산하면 됩니다.
코드
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Solution_1940_가라RC카 { static final int INCREASE = 1; static final int DECREASE = 2; public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for (int tc = 1; tc <= T; tc++) { int N = Integer.parseInt(br.readLine()); int distance = 0, currentSpeed = 0; ; for (int i = 0; i < N; i++) { String str = br.readLine(); if (str.length() == 1) {// 현재 속도 유지 distance += currentSpeed; continue; } // command가 0이 아닌 경우 StringTokenizer st = new StringTokenizer(str, " "); int command = Integer.parseInt(st.nextToken()); int speed = Integer.parseInt(st.nextToken()); switch (command) { case INCREASE:// 가속 currentSpeed += speed; break; case DECREASE:// 감속 currentSpeed -= speed; currentSpeed = currentSpeed < 0 ? 0 : currentSpeed; break; } //속도조절 후 거리계산 distance += currentSpeed; } sb.append("#" + tc + " " + distance + "\n"); } System.out.println(sb); } }
'알고리즘 > SW Expert Academy' 카테고리의 다른 글
1225. [S/W 문제해결 기본] 7일차 - 암호생성기 (0) 2022.03.07 5215. 햄버거 다이어트 (0) 2022.03.03 2805. 농작물 수확하기 - 자바(JAVA) (0) 2022.02.24 2001. 파리 퇴치 - 자바(JAVA) (0) 2022.02.23 1873. 상호의 배틀필드 - 자바(JAVA) (0) 2022.02.22