programmers.co.kr/learn/courses/30/lessons/42840
코딩테스트 연습 - 모의고사
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는
programmers.co.kr
- 단순히 완전탐색을 하는 문제.
더보기
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> answers) {
vector<int> answer;
const int person_1[] = { 1,2,3,4,5 };
const int person_2[] = { 2, 1, 2, 3, 2, 4, 2, 5 };
const int person_3[] = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
const int arrSize_1 = sizeof(person_1) / sizeof(int);
const int arrSize_2 = sizeof(person_2) / sizeof(int);
const int arrSize_3 = sizeof(person_3) / sizeof(int);
int index_1 = 0;
int index_2 = 0;
int index_3 = 0;
int count_1 = 0;
int count_2 = 0;
int count_3 = 0;
for (int idx = 0; idx < answers.size(); idx++)
{
const int ans = answers[idx];
if (person_1[index_1] == ans)
{
count_1++;
}
if (person_2[index_2] == ans)
{
count_2++;
}
if (person_3[index_3] == ans)
{
count_3++;
}
index_1++;
if (arrSize_1 <= index_1)
index_1 = 0;
index_2++;
if (arrSize_2 <= index_2)
index_2 = 0;
index_3++;
if (arrSize_3 <= index_3)
index_3 = 0;
}
const int maxCnt = count_1 > count_2 ? (count_1 > count_3 ? count_1 : count_3)
: (count_2 > count_3 ? count_2 : count_3);
if (maxCnt == count_1)
answer.push_back(1);
if (maxCnt == count_2)
answer.push_back(2);
if (maxCnt == count_3)
answer.push_back(3);
#if 0
printf("%d, %d %d %d\n", maxCnt, count_1, count_2, count_3);
#endif
return answer;
}
int main()
{
vector<int> answers = { 1,3,2,4,2 };
vector<int> answer = solution(answers);
return 0;
}
'Coding Problem > 프로그래머스' 카테고리의 다른 글
[프로그래머스/LV1] - 2016년 (0) | 2021.04.24 |
---|---|
[프로그래머스/LV1] - 체육복 (0) | 2021.04.24 |
[프로그래머스/Lv1] - 완주하지 못한 선수 (0) | 2021.04.24 |
[프로그래머스/Lv1/카카오] - 신규 아이디 추천 (0) | 2021.04.20 |
[프로그래머스/Lv1/카카오] - 크레인 인형뽑기 (0) | 2021.04.19 |