지우너

[프로그래머스] 모의고사 C++ 본문

Problem Solving

[프로그래머스] 모의고사 C++

지옹 2025. 2. 19. 23:22

문제

https://school.programmers.co.kr/learn/courses/30/lessons/42840

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

풀이 및 코드

규칙대로 배열을 만들고 %를 이용하여 배열 범위를 벗어나지 않도록 했다.

정답의 갯수가 score 벡터에 들어가게 되고, maxScore인 사람을 result 벡터에 입력한다.

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> score(3);
    // 12345 12345
    // 21 23 24 25
    // 33 11 22 44 55
    int first[5] = {1, 2, 3, 4, 5};
    int second[8] = {2, 1, 2, 3, 2, 4, 2, 5};
    int third[10] = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
    
    for(int i=0; i<answers.size(); i++){
        int tmp = answers[i];
        if (first[i%5]==tmp) score[0]++;
        if (second[i%8]==tmp) score[1]++;
        if (third[i%10]==tmp) score[2]++;
    }
    
    int maxScore = max({score[0], score[1], score[2]});
    vector<int> result;
    for (int i = 0; i < 3; i++) {
        if (score[i] == maxScore) {
            result.push_back(i + 1);  // 번호는 1부터 시작
        }
    }
    
    return result;
}