본문 바로가기

코딩테스트 준비105

[프로그래머스][C++] 최댓값과 최솟값 #include #include #include using namespace std; string solution(string s) { string answer = ""; vector nums; for(int i = 0; i < s.size(); i++) { int addNum = 0; bool isMinus = false; while(s[i] != ' ' && i < s.size()) { int curNum = s[i] - '0'; // 음수인 경우 if(s[i] == '-') { isMinus = true; i++; continue; } addNum = addNum * 10 + curNum; i++; } if(isMinus) addNum *= -1; nums.push_back(addNum); } so.. 2023. 10. 30.
[프로그래머스][C++] 할 일 목록 1 #include #include using namespace std; vector solution(vector todo_list, vector finished) { vector answer; for(int i = 0; i < todo_list.size(); i++) { if(!finished[i]) { // 일을 못 마침 answer.push_back(todo_list[i]); } } return answer; } 1. finished와 todo_list에서 같은 인덱스를 사용하므로 어느 한쪽의 크기를 받아 반복문을 진행하며 원소에 접근한다. 2. todo_list의 각 값을 finished를 이용해 체크하고, 일을 마치지 못 한 경우 라면 answer에 todo_list의 해당 원소를 넣어줌. 3... 2023. 10. 30.
[프로그래머스][C++] 공원 산책 #include #include #include #include using namespace std; vector solution(vector park, vector routes) { vector answer; map obstacles; // 장애물의 좌표들 // 현재 위치 => {posX, posY} int posX, posY; // 시작 지점과 장애물 탐색 for(int i = 0; i < park.size(); i++) { for(int j = 0; j < park[i].size(); j++) { vector v = {i, j}; // 탐색할 위치 if(park[i][j] == 'S') { // 시작 위치 설정 posX = i; posY = j; } else if(park[i][j] == 'X') .. 2023. 10. 27.
[프로그래머스][C++] 마지막 두 원소 #include #include using namespace std; vector solution(vector num_list) { vector answer = num_list; int lastIndex = num_list.size() - 1; // 마지막 인덱스 int addNum; // 추가할 숫자 if(num_list[lastIndex] > num_list[lastIndex - 1]) { // 마지막 원소 > 마지막 직전 원소 addNum = num_list[lastIndex] - num_list[lastIndex - 1]; } else { // 마지막 원소 2023. 10. 27.
[프로그래머스][C++] 그림 확대 #include #include using namespace std; vector solution(vector picture, int k) { vector answer; for(int i = 0; i < picture.size(); i++) { string expandStr; for(int j = 0; j < picture[i].size(); j++) { for(int n = 0; n < k; n++) { expandStr += picture[i][j]; } } for(int n = 0; n < k; n++) { answer.push_back(expandStr); } } return answer; } 1. picture의 크기만큼 반복하며 매 반복시 추가될 확장한 문자열 expandStr을 선언한다. 2.. 2023. 10. 26.
[프로그래머스][C++] 개인정보 수집 유효기간 #include #include #include using namespace std; vector solution(string today, vector terms, vector privacies) { vector answer; // 현재 날짜 int curYear = (today[2] - '0') * 10 + (today[3]- '0'); int curMonth = (today[5]- '0') * 10 + (today[6]- '0'); int curDay = (today[8]- '0') * 10 + (today[9]- '0'); // 약관 종류별 보관 달 수 map storage; for(int i = 0; i < terms.size(); i++) { if(terms[i].size() == 3) { //.. 2023. 10. 25.
[프로그래머스][C++] 두 수의 연산값 비교하기 #include #include using namespace std; int solution(int a, int b) { int answer = 0; int i = b; int j = 2 * a * b; while(b > 0) { // b의 자릿수에 따라 a에 10을 곱하는 횟수가 정해짐 b /= 10; a *= 10; } i += a; // 최종 i = [a x {10^(b의 자릿수)}] + b answer = i >= j ? i : j; return answer; } #include #include #include using namespace std; int solution(int a, int b) { int answer = 0; int i = b + (a * pow(10, to_string(b).s.. 2023. 10. 25.
[프로그래머스][C++] 수 조작하기 2 #include #include using namespace std; string solution(vector numLog) { string answer = ""; for(int i = 1; i < numLog.size(); i++) { int curDiff = numLog[i] - numLog[i - 1]; if(curDiff == 1) { answer += "w"; } else if(curDiff == -1) { answer += "s"; } else if(curDiff == 10) { answer += "d"; } else if(curDiff == -10) { answer += "a"; } } return answer; } 1. numLog의 원소들의 각 차이를 이용하여 어떤 조작이 있었는지 알 수.. 2023. 10. 24.
[프로그래머스][C++] 바탕화면 정리 #include #include using namespace std; vector solution(vector wallpaper) { vector answer; // 모든 파일의 왼쪽 위 점의 위치를 나타냄 vector Files; for(int i = 0; i 해당 좌표를 넣음 Files.push_back({i,j}); } } } // 1. 가장 왼쪽에 있는 파일 찾기 // 2. 가장 오른쪽에 있는 파일 찾기 // 3. 가장 위쪽에 있는 파일 찾기 // 4. 가장 아래쪽에 있는 파일 찾기 int U.. 2023. 10. 20.
[프로그래머스][C++] 신규 아이디 추천 #include #include using namespace std; string solution(string new_id) { for(int i = 0; i = '0' && new_id[i] = 'a' && new_id[i] = 'A' && new_id[i] 2023. 10. 20.
[프로그래머스][C++] 배열 만들기 3 #include #include using namespace std; vector solution(vector arr, vector intervals) { vector answer; int first_begin = intervals[0][0]; int first_end = intervals[0][1]; int second_begin = intervals[1][0]; int second_end = intervals[1][1]; for(int i = first_begin; i 2023. 10. 20.
[프로그래머스][C++] 홀짝 구분하기 #include using namespace std; int main(void) { int n; cin >> n; cout 2023. 10. 19.