코딩테스트 연습4 [프로그래머스][C++] 하노이의 탑 #include #include using namespace std; vector hanoi(int curNum, int n, vector pred) { if(curNum == n) return pred; curNum++; vector curVec = pred; // 1. 이전 원판들을 2번으로 옮김(2와 3을 바꿈) for(int i = 0; i < pred.size(); i++) { if(pred[i][0] == 2) { pred[i][0] = 3; } else if(pred[i][0] == 3) { pred[i][0] = 2; } if(pred[i][1] == 2) { pred[i][1] = 3; } else if(pred[i][1] == 3) { pred[i][1] = 2; } } // 2. k번.. 2023. 10. 13. [프로그래머스][C++] 덧칠하기 #include #include using namespace std; int solution(int n, int m, vector section) { int answer = 0; int index = 0; for(int i = section[index++]; i 리턴 if(index >= section.size()) return answer; } i = section[index]; // 현재 섹션을 반복자로 재정의 } return answer; } 1. answer과 index를 선언 2. 반복자 i가 섹션의 0번째 인덱스부터 시작해 n이 될 때까지 반복을 진행한다. 3. 매 반복은 페인트칠을 하는 횟수를 의미하므로 answer을 1씩 증가시킨다. 4. 이후 섹션의 다음 수가 현재 수에 롤러의 길이를 더.. 2023. 9. 13. [프로그래머스][C++] 카드 뭉치 #include #include using namespace std; string solution(vector cards1, vector cards2, vector goal) { string answer = "Yes"; int x = 0; // card1에서 나와야하는 단어의 인덱스 번호 int y = 0; // card2에서 나와야하는 단어의 인덱스 번호 for(int i = 0 ; i < goal.size(); i++) { string curWord = goal[i]; if(curWord == cards1[x]) { // 현재 단어가 카드뭉치 1에 있는 경우 x++; } else if(curWord == cards2[y]) { // 현재 단어가 카드뭉치 2에 있는 경우 y++; } else { ans.. 2023. 9. 12. [프로그래머스][C++] 추억 점수 #include #include #include using namespace std; vector solution(vector name, vector yearning, vector photo) { vector answer; map scores; for(int i = 0 ; i < name.size(); i++) { // 이름별 그리움 점수 저장 scores[name[i]] = yearning[i]; } for(int i = 0; i < photo.size(); i++) { answer.push_back({0}); for(int j = 0; j < photo[i].size(); j++) { string curName = photo[i][j]; answer[i] += scores[curName]; } } r.. 2023. 9. 12. 이전 1 다음