Posts [프로그래머스] 모의고사 / C++
Post
Cancel

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


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


  1. 1~3번 수포자들이 정답을 맞힌 개수를 chk 벡터에 담아줍니다.

  2. 그 중 가장 높은 점수를 찾아줍니다.

  3. 최고점을 받은 학생이 여럿일 수 있으므로, 동점자는 answer 벡터에 순차적으로 담아줍니다.


최댓값을 구하는 max 함수는 algorithm 헤더에 있습니다.


소스 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> chk(3,0); // 0으로 초기화된 3개의 원소를 가진 벡터 (정답 맞힌 개수)
    
    int A[] = {1,2,3,4,5};
    int B[] = {2,1,2,3,2,4,2,5};
    int C[] = {3,3,1,1,2,2,4,4,5,5};
    
    for(int i=0; i<answers.size(); i++){
        if(answers[i]==A[i%5])
            chk[0]++;
        if(answers[i]==B[i%8])
            chk[1]++;
        if(answers[i]==C[i%10])
            chk[2]++;
    }
    
    int high_score = max(max(chk[0], chk[1]), chk[2]);

    for(int i=0; i<3; i++){
        if(chk[i]==high_score){
            answer.push_back(i+1);
        }
    }
    
    return answer;
}



This post is licensed under CC BY 4.0 by the author.

[프로그래머스] 문자열을 정수로 바꾸기 / C++

[WebLogic] 웹로직 12c 패치 - OPatch

Comments powered by Disqus.