[프로그래머스 문제풀이] 19. 모의고사

WIGWAG·2023년 1월 4일
0

프로그래머스

목록 보기
19/32

나의 풀이

map을 이용하여 점수를 키로, 학생의 인덱스를 놓고 점수가 높은 순으로 정렬하였다.
학생은 어차피 순서대로 참조하기 때문에 정렬할 필요가 없다.

map<int, vector<int>, greater<int>> score_man;

이렇게 map에 데이터를 다 저장해 놓으면 스코어가 가장 큰 첫 번째 원소만 빼내면 되는데
map 내부 메서드 중 front같은 함수가 없어서 begin으로 가져왔다.

score_man.begin()->second

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

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<vector<int>> mans_loop = { { 1,2,3,4,5 }, { 2,1,2,3,2,4,2,5 }, { 3,3,1,1,2,2,4,4,5,5 } };

    map<int, vector<int>, greater<int>> score_man;

    for (int man = 0; man < 3; ++man)
    {
        int score = 0;
        for (size_t i = 0; i < answers.size(); ++i)
        {
            if (mans_loop[man][i % mans_loop[man].size()] == answers[i])
            {
                ++score;
            }
        }
        score_man[score].push_back(man);
    }

    vector<int> scores;
    for (auto man : score_man.begin()->second)
    {
        scores.push_back(man + 1);
    }
    return scores;
}

추천을 가장 많이 받은 풀이

미리 점수를 측정하는 3칸을 보유한 벡터를 만들어 놓고
루프를 돌면서 정답을 맞추면 해당 인덱스를 +1한다.

그 후 점수가 가장 큰 학생을 가져와서 점수 벡터에 가장 큰 학생이 있는 지 확인하고
있다면 정답 벡터에 넣는다.

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

using namespace std;

vector<int> one = {1,2,3,4,5};
vector<int> two = {2,1,2,3,2,4,2,5};
vector<int> thr = {3,3,1,1,2,2,4,4,5,5};

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> they(3);
    for(int i=0; i<answers.size(); i++) {
        if(answers[i] == one[i%one.size()]) they[0]++;
        if(answers[i] == two[i%two.size()]) they[1]++;
        if(answers[i] == thr[i%thr.size()]) they[2]++;
    }
    int they_max = *max_element(they.begin(),they.end());
    for(int i = 0; i< 3; i++) {
        if(they[i] == they_max) answer.push_back(i+1);
    }
    return answer;
}

모의고사 문제 링크

profile
윅왁의 프로그래밍 개발노트

0개의 댓글