본문 바로가기

c++92

[프로그래머스][C++] n 번째 원소까지 #include #include using namespace std; vector solution(vector num_list, int n) { vector answer; for(int i = 0; i < n; i++) { answer.push_back(num_list[i]); } return answer; } 1. 0부터 주어진 수 n까지 반복하며 answer에 num_list의 원소들을 넣어준다. 2. answer을 리턴한다. 이 문제를 몰라서 이 글을 볼 사람은 거의 없을 것 같다.ㅋㅋ 그 정도로 매우 쉬웠다! 2023. 10. 31.
[프로그래머스][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.
[알고리즘] DFS와 BFS 1. DFS(Depth-First Search) - 깊이 우선 탐색 - 루트 노드나 임의 노드에서 "다음 브랜치로 넘어가기 전에 해당 브랜치를 모두 탐색"하는 방법 - 스택이나 재귀함수를 이용해 구현 - 모든 경로를 방문해야 할 경우 적합 - 인접 행렬 : O(V^2) - 인접 리스트 : O(V+E) // V는 접점, E는 간선 2. BFS(Breadth-First Search) - 너비 우선 탐색 - 루트 노드나 임의 노드에서 "인접한 노드부터 먼저 탐색"하는 방법 - 큐를 이용해 구현 - 최소 비용으로 탐색을 진행할 때 적합\ - 인접 행렬 : O(V^2) - 인접 리스트 : O(V+E) 3. 소스코드(C++) // DFS #include #include using namespace std; int .. 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.
[알고리즘] 해시 테이블(Hash Table) 1. 해시 테이블 - 완전 탐색(브루트 포스)으로 시간초과에 빠지게 되는 문제들을 풀기 위해 필요함 - Key, Value로 데이터를 저장하는 자료구조 - 데이터를 빠르게 검색할 수 있음 - 각 key에 따라 고유한 index를 생성하고 이 index 값을 활용해 값을 저장하거나 검색한다. - 미리 key값으로 데이터들을 저장해 놓으면 데이터를 찾기 쉽다. - 평균 시간복잡도 : O(1) 2. 해시 함수에서 고유 인덱스 값을 설정하는 방법 1) Division Method - 나눗셈을 이용하는 방법 - 입력값을 테이블의 크기로 나누어 계산 - 테이블의 크기를 소수로 정하고 2의 제곱수와 먼 값을 사용해야 효과가 좋다고 알려짐 2) Digit Folding - 각 key의 문자열을 ASCII 코드로 바꾸고.. 2023. 10. 24.
[C++] final 키워드 2 이것 저것 알아보면서 코드를 보면 final이라는 키워드가 있는데 이게 뭐지 싶었다. 알아보니 final은 c/c++에서는 쓰이지 않고, java에서 쓰이는 것이라고 한다. java에서 변수 앞에 쓰이는 final은 c++에서의 const와 같은 상수를 뜻한다! //요약 final은 java에서 쓰임 변수 앞에 쓰인 fianl은 c++의 const와 같은 의미로 사용 2023. 10. 24.
[프로그래머스][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.