일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Camera Movement
- css framework
- Digital Ocean
- Unity Editor
- Spring Boot
- Git
- critical rendering path
- express
- Camera Zoom
- Unity IAP
- docker
- SDK upgrade
- screencapture
- unity
- server
- spread 연산자
- MySQL
- springboot
- OverTheWire
- Google Refund
- linux
- Packet Network
- Google Developer API
- java
- mongoDB
- draganddrop
- rpg server
- react
- nodejs
- --watch
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 1. 동영상 재생기 본문
https://school.programmers.co.kr/learn/courses/30/lessons/340213
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
당신은 동영상 재생기를 만들고 있습니다. 당신의 동영상 재생기는 10초 전으로 이동, 10초 후로 이동, 오프닝 건너뛰기 3가지 기능을 지원합니다. 각 기능이 수행하는 작업은 다음과 같습니다.
- 10초 전으로 이동: 사용자가 "prev" 명령을 입력할 경우 동영상의 재생 위치를 현재 위치에서 10초 전으로 이동합니다. 현재 위치가 10초 미만인 경우 영상의 처음 위치로 이동합니다. 영상의 처음 위치는 0분 0초입니다.
- 10초 후로 이동: 사용자가 "next" 명령을 입력할 경우 동영상의 재생 위치를 현재 위치에서 10초 후로 이동합니다. 동영상의 남은 시간이 10초 미만일 경우 영상의 마지막 위치로 이동합니다. 영상의 마지막 위치는 동영상의 길이와 같습니다.
- 오프닝 건너뛰기: 현재 재생 위치가 오프닝 구간(
op_start
≤ 현재 재생 위치 ≤op_end
)인 경우 자동으로 오프닝이 끝나는 위치로 이동합니다.
동영상의 길이를 나타내는 문자열 video_len
, 기능이 수행되기 직전의 재생위치를 나타내는 문자열 pos
, 오프닝 시작 시각을 나타내는 문자열 op_start
, 오프닝이 끝나는 시각을 나타내는 문자열 op_end
, 사용자의 입력을 나타내는 1차원 문자열 배열 commands
가 매개변수로 주어집니다. 이때 사용자의 입력이 모두 끝난 후 동영상의 위치를 "mm
:ss
" 형식으로 return 하도록 solution 함수를 완성해 주세요.
풀이
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string secToStr(int sec);
string solution(string video_len, string pos, string op_start, string op_end, vector<string> commands) {
int video_sec = stoi(video_len.substr(0,2)) * 60 + stoi(video_len.substr(3));
int op_start_sec = stoi(op_start.substr(0,2)) * 60 + stoi(op_start.substr(3));
int op_end_sec = stoi(op_end.substr(0,2)) * 60 + stoi(op_end.substr(3));
int current_sec = stoi(pos.substr(0,2)) * 60 + stoi(pos.substr(3));
if(op_start_sec <= current_sec && current_sec < op_end_sec ) current_sec = op_end_sec;
for(int i = 0; i<commands.size(); i++){
if(commands[i] == "prev"){
current_sec = current_sec < 10 ? 0 : current_sec - 10;
}
else if(commands[i] == "next"){
current_sec = current_sec +10 > video_sec ? video_sec : current_sec + 10;
}
if(op_start_sec <= current_sec && current_sec < op_end_sec ) current_sec = op_end_sec;
cout << secToStr(current_sec) <<endl;
}
string result = secToStr(current_sec);
return result;
}
string secToStr(int sec){
int minute = sec/60;
int second = sec%60;
string result = (minute <10 ? "0"+to_string(minute) : to_string(minute)) + ":"+(second <10 ? "0"+to_string(second):to_string(second));
return result;
}
느낀점 / 배울점
언어가 다르니까, 내장함수를 이용해야하는 부분이 굉장히 헷갈린다.
string video_length = "33:00"; 일때 33과 00 을 int로 뽑는 부분
video_length.substr(0,2) * 60 + video_length.substr(3);
int를 string으로 변환하고, 두자리수로 표현하는 부분
string result = (minute <10 ? "0"+to_string(minute) : to_string(minute))
오프닝 건너뛰기가 가장 힘들었는데, 건너뛰기 명령어가 들어왔을때 수행하는 것이 아니라, 명령어 수행 후 조건에 맞으면 건너뛰기를 자동으로 수행해야 했다. 또, 첫 번째 명령어를 수행하기 전에도 오프닝 건너뛰기를 해야했다.
이부분이 가장 혼란스러웠으나, for문 앞에서 한번 수행해주고, for문 블록 내부에서 한번 수행해줌으로써 문제를 해결했다.
c#을 사용하다보니, 함수선언 위치를 잘 신경쓰지 않았는데 여기서는 함수를 맨 위에 선언해줘야하는걸 매번 잊는다.. 점차 적응되겠지?
'Algorithm(c++) > Level 1' 카테고리의 다른 글
[프로그래머스] Level 1. 공원 (0) | 2025.01.17 |
---|---|
[프로그래머스] Level 1. 데이터 분석 (0) | 2025.01.16 |
[프로그래머스] Level 1. 방문길이 (0) | 2025.01.16 |
[프로그래머스] Level 1. 실패율 (1) | 2025.01.15 |