강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

robin님의 프로필 이미지
robin

작성한 질문수

10주완성 C++ 코딩테스트 | 알고리즘 코딩테스트

2-L

2-L 자바로 작성한 코드입니다.

작성

·

328

0

package lecture2;

import java.util.Scanner;

public class Prob2452 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int totalWinTime = 0;
        int totalWinTime2 = 0;
        int startWinTime =0;
        int endWinTime = 0;
        int startWinTime2=0;
        int endWinTime2=0;
        int score = 0,score2 = 0;
        for (int i = 0; i < n; i++) {
            int goalTeam = sc.nextInt();
            if(goalTeam == 1)score++;
            else score2++;
            String str = sc.next();
            if(score-score2==1 && goalTeam==1){
                startWinTime = Integer.parseInt(str.split(":")[0])*60 + Integer.parseInt(str.split(":")[1]);
            }else if(score2-score==1 && goalTeam ==2){
                startWinTime2 = Integer.parseInt(str.split(":")[0])*60 + Integer.parseInt(str.split(":")[1]);
            }
            if(score == score2){
                if(goalTeam==1){
                    endWinTime2 = Integer.parseInt(str.split(":")[0])*60 + Integer.parseInt(str.split(":")[1]);
                    totalWinTime2 += endWinTime2 - startWinTime2;
                }else{
                    endWinTime = Integer.parseInt(str.split(":")[0])*60 + Integer.parseInt(str.split(":")[1]);
                    totalWinTime += endWinTime - startWinTime;
                }
            }
        }
        if(score>score2){
            totalWinTime+=48*60 - startWinTime;
        }else if(score2>score){
            totalWinTime2+=48*60 - startWinTime2;
        }
        System.out.printf("%02d:%02d\n",totalWinTime/60,totalWinTime%60);
        System.out.printf("%02d:%02d\n",totalWinTime2/60,totalWinTime2%60);
    }
}

이기기 시작한 순간(자신이 골을 넣고, 점수차이가 1인 순간)을 저장하고, 비긴 순간에 총 이기고 있던 시간에 계산하여 더해주는 구조입니다.

Java의 LocalTime 등의 시간 관련 클래스들을 사용하려다가 실패해서 40분 날려먹고 10분만에 작성한 코드입니다.

제대로 모르는 클래스를 문제풀이에 사용하는 것 보다 그냥 필요한 만큼만 구현하는게 더 빠른 경우가 많은 것을 깨달았습니다.

답변 1

0

큰돌님의 프로필 이미지
큰돌
지식공유자

LGTM

robin님의 프로필 이미지
robin

작성한 질문수

질문하기