새소식

코딩테스트/프로그래머스

[C++] 모의고사

  • -

[문제]

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


[문제 풀이]

얼핏보면 규칙을 이용해서 찾으라는건가? 하고 헷갈릴 수 있지만 간단하게 규칙이 끝나는 지점까지만 모두 기억해두고 해당 부분과 일치하는지를 반복문을 통해서 찾아보면 된다.


[코드]

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

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> math_student_1 = {1,2,3,4,5};
    vector<int> math_student_2 = {2,1,2,3,2,4,2,5};
    vector<int> math_student_3 = {3,3,1,1,2,2,4,4,5,5};
    int count_1 = 0;
    int count_2 = 0;
    int count_3 = 0;
    int max_count = 0;
    
    
    for(int i = 0;i<answers.size();i++){
        if(answers[i] == math_student_1[i % math_student_1.size()]){
            count_1++;
        }
        if(answers[i] == math_student_2[i % math_student_2.size()]){
            count_2++;
        }
        if(answers[i] == math_student_3[i % math_student_3.size()]){
            count_3++;
        }
    }

    max_count = max(count_1, count_2);
    max_count = max(max_count, count_3);
    if(max_count == count_1){
        answer.push_back(1);
    }
    if(max_count == count_2){
        answer.push_back(2);
    }
    if(max_count == count_3){
        answer.push_back(3);
    }
    
    return answer;
}

*answers와 answer을 착각해서 도중에 실수가 있었다. 변수명은 언제나 주의하자.

 

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

[C++] 위장  (0) 2022.12.03
[C++] 전화번호 목록  (0) 2022.12.03
[C++] 포켓몬  (0) 2022.12.03
[C++] 완주하지 못한 선수  (0) 2022.12.03
[C++] 최소직사각형  (0) 2022.10.15
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.