프로그래머스 - lv1 모의고사

이상현·2021년 1월 2일
0

알고리즘_문제풀이

목록 보기
7/45
post-thumbnail

모의고사

문제는 프로그래머스에서 확인 할 수 있다.


✔ 접근방법

모든 경우를 판단, 완전 탐색

  1. std1, std2, std3 의 정답을 비교
  2. 맞은 갯수를 map<std번호, 정답개수>에 저장
  3. map을 value값을 기준으로 정렬
    3-1 map->vector로 전환
    3-2 비교함수 정의
    3-3 sort() 이용하여 정렬
  4. 가장 높은 점수를 받은 사람 리턴

✔ 코드

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

using namespace std;

/* sort에서 사용할 비교 함수 */
bool cmp(const pair<int,int> &a, const pair<int, int> &b){
    if (a.second == b.second ) return a.first<b.first;
    return a.second > b.second;
}

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> std1 = {1,2,3,4,5};
    vector<int> std2 = {2,1,2,3,2,4,2,5};
    vector<int> std3 = {3,3,1,1,2,2,4,4,5,5};

    map<int, int> score;
    int max = 0;

    for( int i=0; i<answers.size(); i++ ){
        /* std의 원소 갯수를 생각해서 % 연산 */
        if(answers[i] == std1[i%5]){score[1]++;}
        if(answers[i] == std2[i%8]){score[2]++;}
        if(answers[i] == std3[i%10]){score[3]++;}
    }

    /* 모두의 답이 틀린경우 segmetaion falut 발생. 이를 방지 */
    if( score.empty() != true ){
        /* map을 vector로 변환, value를 이용한 정렬 */
        vector<pair<int, int>> v( score.begin(), score.end() );

        sort(v.begin(), v.end(), cmp);
        max = v.begin()->second;

        for( int i=0; i<v.size(); i++){
            if( max == v[i].second ){
                answer.push_back(v[i].first);
            }
        }
    }

    return answer;
}

int main(void){
    
    vector<int> answers = {1,2,3,4,5};
    vector<int> ret;

    ret = solution(answers);

    for( auto elem : ret ){
        cout << elem << endl;
    }

    return 0;
}

☝ 팁

  • sort에서 이용하는 비교함수 정의 기본형을 기억하자
  • map -> vector 전환 시 문법
    vector<pair<int, int>> v (score.begin(), score.end());

다른 방식의 풀이

문제를 해결하고 다른 사람들의 풀이를 보니, 나는 최대값을 가진 value를 구하기위해 sort() 하는 방식 외에도 max_element() 함수를 사용하여 최대값을 가진 value를 구하는 방식도 존재했다.

이를 참고하여 다시 코드를 작성하였다.


✔ 접근방법

모든 경우를 판단, 완전 탐색

  1. std1, std2, std3 의 정답을 비교
  2. 맞은 갯수를 map<std번호, 정답개수> 벡터에 저장
    3. map을 value값을 기준으로 정렬
    3-1 map->vector로 전환
    3-2 비교함수 정의
    3-3 sort() 이용하여 정렬
  3. max_element를 이용하여 가장 높은 점수 계산
  4. 가장 높은 점수를 받은 사람 리턴

✔ 코드

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

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> std1 = {1,2,3,4,5};
    vector<int> std2 = {2,1,2,3,2,4,2,5};
    vector<int> std3 = {3,3,1,1,2,2,4,4,5,5};

    vector<int> score(3);
    int max = 0;

    for( int i=0; i<answers.size(); i++ ){
        /* std의 원소 갯수를 생각해서 % 연산 */
        if(answers[i] == std1[i%5]){score[0]++;}
        if(answers[i] == std2[i%8]){score[1]++;}
        if(answers[i] == std3[i%10]){score[2]++;}
    }

    if( score.empty() != true ){
        /* score 벡터에서 가장 높은 점수를 취득 */
        max = *max_element(score.begin(), score.end());
        
        /* 가장 높은 점수를 가진 사람 확인 */
        for ( int i=0; i<score.size(); i++ ){
            if( max == score[i] ){
                answer.push_back(i+1);
            }
        }
    }

    return answer;
}

int main(void){
    
    vector<int> answers = {1,3,2,4,2};
    vector<int> ret;

    ret = solution(answers);

    for( auto elem : ret ){
        cout << elem << endl;
    }

    return 0;
}

☝ 팁

  • max_element()는 구간 안에서(배열이나 벡터 등) 최대, 최소값을 구한다. 이때 값 자체를 리턴하지 않고 주소 또는 이터레이터를 리턴한다.

👍 참고 사이트

profile
'당신을 한 줄로 소개해보세요'를 이 블로그로 대신 해볼까합니다.

0개의 댓글

관련 채용 정보