C++로 해결하였다.
1. Solution
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int stu1[5] = {1, 2, 3, 4, 5};
int stu2[8] = {2, 1, 2, 3, 2, 4, 2, 5};
int stu3[10] = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
vector<int> solution(vector<int> answers) {
vector<int> answer;
int score[3];
fill(score, score + 3, 0);
// 문제 체점
for(int i = 0; i < answers.size(); i++){
if(answers[i] == stu1[i % 5]) score[0] += 1;
if(answers[i] == stu2[i % 8]) score[1] += 1;
if(answers[i] == stu3[i % 10]) score[2] += 1;
}
// 가장 많은 문제를 맞힌 사람 탐색
int maxScore = max(max(score[0], score[1]), score[2]); // 또는 maxScore = *max_element(score, score + 3)
for(int stu = 0; stu < 3; stu++){
if(score[stu] == maxScore){
answer.push_back(stu + 1);
}
}
return answer;
}
2. 알아야 할 개념
max()
: max(비교대상1, 비교대상2);
만약, 배열/벡터에서 특정 범위 내의 최대/최소 값을 찾고 싶다면 max_element(a, a+ 10) 사용하기 (시간은 더 느려지는 것 같다)
max_element() 사용방법
: *max_element(시작위치, 끝위치)
예시) *max_element(b.begin(), b.end())
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] MySQL - JOIN 있었는데요 없었습니다 (0) | 2022.04.09 |
---|---|
[프로그래머스] MySQL - JOIN 없어진 기록 찾기 (0) | 2022.04.09 |
[KaKao 기출] 직사각형 좌표 값 구하기 (0) | 2022.03.11 |
프로그래머스(11일 이내 해결예정) - C++ 124 나라의 숫자 (0) | 2021.10.11 |
프로그래머스(미해결) - C++ 소수찾기 (0) | 2021.10.09 |
댓글