SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
www.swexpertacademy.com
풀이
- command를 입력받아서 0이 아닌 경우(1이나 2인 경우) 에만 따로 처리를 해준다.
- 1 인경우(가속) 기존 속도에 가속도를 더해준다.
- 2 인경우(감속) 기존 속도에서 가속도를 빼준다. (문제의 조건에 따라 가속도가 기존 속도보다 클경우에는 속도는 0이 된다.)
- 매 초마다 속도만큼을 거리에 더해준다(초속이므로)
- 시간이 모두 끝난 후 거리를 출력
import java.io.*;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int tc=1; tc<=T; tc++) {
int time = Integer.parseInt(br.readLine()); //시간
int rc = 0; //속도
int dis = 0; //거리
for(int i=0; i<time; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int command = Integer.parseInt(st.nextToken());
if(command != 0) {
int accel = Integer.parseInt(st.nextToken()); //가속도
if(command == 1) { //가속
rc += accel;
} else { //감속
if(rc < accel) {
rc = 0;
} else {
rc -= accel;
}
}
}
dis += rc;
}
System.out.println("#"+tc+" "+dis);
}
}
}
'SWEA > D2' 카테고리의 다른 글
| [SWEA] 1928. Base64 Decoder (0) | 2019.05.16 |
|---|---|
| [SWEA] 1859. 백만 장자 프로젝트 (1) | 2019.05.16 |
| [SWEA] 1945. 간단한 소인수분해 (0) | 2019.05.14 |
| [SWEA] 1946. 간단한 압축 풀기 (0) | 2019.05.14 |
| [SWEA] 1948. 날짜 계산기 (0) | 2019.05.14 |