PCCP 기출문제[PCCP 기출문제] 1번 / 동영상 재생기 - JAVA

2025. 3. 5. 14:14백준 및 프로그래머스/프로그래머스 LV 1

문제 설명

 

제한사항

 

입출력 예

 

문제 설명
1.동영상 재생기를 만들고 있습니다. 기능은 10초 전 이전, 10초 후 이동 기능이 있습니다.
2.현재 시각이 주어집니다. 10초 전으로 이전했는데 0초 미만이면 0초로 이동합니다.
3.10초 후로 이동하는데 동영상 끝나는 길이보다 길다면 동영상이 끝나는 위치 시간으로 이동합니다.
4.이 과정에서 오프닝 사이에 동영상이 있다면 오프닝이 끝나는 위치로 이동해줍니다.

5.순서대로 주어진 기능을 사용한 후 어디 시각에서 동영상이 멈추는 지 출력해주면 됩니다.

 

전체 코드

//10초전 - 사용자가 prev입력, 10초전으로 단 10초 미만인경우 영상의 처음 위치
//10초후 - next입력하면 10초 후 단 남은 시간이 10초 미만일 경우 마지막 위치
//오프닝 건너뛰기 - 오프닝이 끝나는 위치로 이동

class Solution {
    static int creattime(String videotime){
        String[] word = videotime.split(":");
        int a = Integer.parseInt(word[0]);
        int b = Integer.parseInt(word[1]);
        return a * 60 + b;
    }
    public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
        String answer = "";
        int videotime = creattime(video_len);
        int postime = creattime(pos);
        int opstarttime = creattime(op_start);
        int opendtime = creattime(op_end);
        if(postime >= opstarttime && postime<=opendtime){
            postime = opendtime;
        }
    
        for(int i = 0; i<commands.length; i++){
            String word = commands[i];
            if(word.equals("next")){
                if(videotime - postime < 10){
                    postime = videotime;
                } else{
                    postime+=10;
                    if(postime >= opstarttime && postime<=opendtime){
                        postime = opendtime;
                    }
                }
            }else if(word.equals("prev")){
                if(postime - 10 <0){
                    postime = 0 ;
                    if(postime >= opstarttime && postime<=opendtime){
                        postime = opendtime;
                    }
                }else{
                    postime-=10;
                    if(postime >= opstarttime && postime<=opendtime){
                        postime = opendtime;
                    }
                }
            }
        }
        int h = postime / 60;
        int m = postime % 60;
        if(h < 10){
            answer+="0";
            answer+=h;
            answer+=":";
        }else{
            answer+=h;
            answer+=":";
        }
        if(m < 10){
            answer+="0";
            answer+=m;
        }else{
            answer+=m;
        }
        
        
        return answer;
    }
}