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

갭라·2021년 6월 24일
0

문제-모의고사

🤔문제 해석

수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.

1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return

❗ 제한 조건

-시험은 최대 10,000 문제로 구성
-문제의 정답은 1,2,3,4,5 중 하나
-가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순으로 정렬

😏풀이

수포자 별로 찍는 순서들을 배열에 저장해둔다.
%를 활용하여 찍는 순서들이 들어있는 배열의 길이만큼 반복문을 돌아 각자의 cnt값을 구한다.
cnt가 가장 큰 사람을 answer에 push_back한다.

💻코드

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

using namespace std;
vector<int> solution(vector<int> answers) {
    vector<int> answer;
    int siz = answers.size();

    vector<int> ans1;
    vector<int> ans2;
    vector<int> ans3;

    int cnt1 = 0;
    int cnt2 = 0;
    int cnt3 = 0;

    int arr1[5] = { 1,2,3,4,5 };
    int arr2[8] = { 2,1,2,3,2,4,2,5 };
    int arr3[10] = { 3,3,1,1,2,2,4,4,5,5 };

    for (int i = 0; i < siz; i++) {
        ans1.push_back(arr1[i % 5]);
        if (ans1[i] == answers[i]) cnt1++;
    }

    for (int i = 0; i < siz; i++) {
        ans2.push_back(arr2[i % 8]);
        if (ans2[i] == answers[i]) cnt2++;
    }

    for (int i = 0; i < siz; i++) {
        ans3.push_back(arr3[i % 10]);
        if (ans3[i] == answers[i]) cnt3++;
    }

    int max = cnt1;
    answer.push_back(1);

    if (cnt2 > max) {
        answer.clear();
        answer.push_back(2);
        max = cnt2;
    }
    else if (cnt2 == max) {
        answer.push_back(2);
    }
    
    if (cnt3 > max) {
        answer.clear();
        answer.push_back(3);
        max = cnt3;
    }
    else if (cnt3 == max) {
        answer.push_back(3);
    }

    return answer;
}
profile
준비생!

0개의 댓글