문제 링크입니다: https://school.programmers.co.kr/learn/courses/30/lessons/388352?language=cpp
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
서로 다른 5개의 번호를 입력하는 문제이므로 5중 반복문을 이용해 풀면 되는 구현 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
using namespace std; | |
int solution(int n, vector<vector<int>> q, vector<int> ans) { | |
int answer = 0; | |
for (int a = 1; a <= n - 4; a++) | |
{ | |
for (int b = a + 1; b <= n - 3; b++) | |
{ | |
for (int c = b + 1; c <= n - 2; c++) | |
{ | |
for (int d = c + 1; d <= n - 1; d++) | |
{ | |
for (int e = d + 1; e <= n; e++) | |
{ | |
vector<int> candidate = {a, b, c, d, e}; | |
bool valid = true; | |
for (int i = 0; i < q.size(); i++) | |
{ | |
int common = 0; | |
for (int num : candidate) | |
{ | |
for (int qnum : q[i]) | |
{ | |
if (num == qnum) | |
{ | |
common++; | |
break; | |
} | |
} | |
} | |
if (common != ans[i]) | |
{ | |
valid = false; | |
break; | |
} | |
} | |
if (valid) | |
{ | |
answer++; | |
} | |
} | |
} | |
} | |
} | |
} | |
return answer; | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영합니다! 질문 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 홀짝트리 (10) | 2025.02.14 |
---|---|
[Programmers] 지게차와 크레인 (0) | 2025.02.13 |
[Programmers] 안티세포 (0) | 2025.02.10 |
[Programmers] 미로 탈출 (0) | 2025.02.08 |
[Programmers] 매출 하락 최소화 (0) | 2025.02.05 |