문제
동혁이는 NBA 농구 경기를 즐겨 본다. 동혁이는 골이 들어갈 때 마다 골이 들어간 시간과 팀을 적는 이상한 취미를 가지고 있다.
농구 경기는 정확히 48분동안 진행된다. 각 팀이 몇 분동안 이기고 있었는지 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 골이 들어간 횟수 N(1<=N<=100)이 주어진다. 둘째 줄부터 N개의 줄에 득점 정보가 주어진다. 득점 정보는 득점한 팀의 번호와 득점한 시간으로 이루어져 있다. 팀 번호는 1 또는 2이다. 득점한 시간은 MM:SS(분:초) 형식이며, 분과 초가 한자리 일 경우 첫째자리가 0이다. 분은 0보다 크거나 같고, 47보다 작거나 같으며, 초는 0보다 크거나 같고, 59보다 작거나 같다. 득점 시간이 겹치는 경우는 없다.
출력
첫째 줄에 1번 팀이 이기고 있던 시간, 둘째 줄에 2번 팀이 이기고 있던 시간을 출력한다. 시간은 입력과 같은 형식(MM:SS)으로 출력한다.
나의 풀이
시간을 초로 변환해서 계산하는게 포인트였던거 같습니다.
나의 코드
import java.io.*;
import java.util.*;
/**
* 백준 2852 - NBA 농구 (실버 3)
* <a href="https://www.acmicpc.net/problem/2852">...</a>
*/
public class Main {
static class ScoreInfo {
int teamNo;
int time; // 초 단위로 저장
public ScoreInfo(int teamNo, String timeStr) {
this.teamNo = teamNo;
this.time = toSeconds(timeStr);
}
}
public static int toSeconds(String time) {
String[] parts = time.split(":");
return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
}
public static String toTimeFormat(int totalSeconds) {
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
return String.format("%02d:%02d", minutes, seconds);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
List<ScoreInfo> scores = new ArrayList<>();
for (int i = 0; i < N; i++) {
String[] input = br.readLine().split(" ");
int teamNo = Integer.parseInt(input[0]);
String timeStr = input[1];
scores.add(new ScoreInfo(teamNo, timeStr));
}
int score1 = 0, score2 = 0;
int winTime1 = 0, winTime2 = 0;
int prevTime = 0;
for (ScoreInfo score : scores) {
int curTime = score.time;
if (score1 > score2) {
winTime1 += curTime - prevTime;
} else if (score2 > score1) {
winTime2 += curTime - prevTime;
}
if (score.teamNo == 1) {
score1++;
} else {
score2++;
}
prevTime = curTime;
}
int endTime = toSeconds("48:00");
if (score1 > score2) {
winTime1 += endTime - prevTime;
} else if (score2 > score1) {
winTime2 += endTime - prevTime;
}
System.out.println(toTimeFormat(winTime1));
System.out.println(toTimeFormat(winTime2));
}
}