본문 바로가기

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

[프로그래머스] 성격 유형 검사하기

728x90
반응형


🔗 문제 링크 

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

 

프로그래머스

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

programmers.co.kr

👩‍💻 코드

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

using namespace std;

string solution(vector<string> survey, vector<int> choices) {
    string answer = "";
    vector<char> indicators = { 'R', 'T', 'C', 'F', 'J', 'M', 'A', 'N' };
    vector<int> scores = { 0, 0, 0, 0, 0, 0, 0, 0 };
    
    for (int i=0; i<choices.size(); i++){
        if(choices[i] == 4){
            continue;
        }
        
        if(choices[i] < 4){
            int index = find(indicators.begin(), indicators.end(), survey[i][0]) - indicators.begin();
            scores[index] += 4-choices[i];
        }else{
            int index = find(indicators.begin(), indicators.end(), survey[i][1]) - indicators.begin();
            scores[index] += choices[i] - 4;
        }
    }
    
    for (int i=0; i<4; i++){
        int first = scores[i*2];
        int second = scores[i*2+1];
        
        if (first >= second){
            answer += indicators[i*2];
        } else{
            answer += indicators[i*2+1];
        }
    }
    
    return answer;
}

✏️ 풀이

점수를 매겨 저장하는 로직과 4가지 성격 유형을 비교하는 로직으로 나누어 풀었다.

1. 비동의일 경우 앞의 성격 유형에 점수를 추가하고 동의일 경우 뒤의 성격 유형에 점수를 추가한다.
2. 최종으로 4가지 성격 유형의 점수를 비교하여 answer에 유형을 추가한다.

 

유형과 점수를 2개의 vector로 나누어 인덱스를 기준으로 저장하였는데 풀고나니 2중 배열이나 map을 사용하는게 더 적합해보인다.

지금 보니까 두번째 로직도 더 간단하게 작성가능한데 왜 저렇게 했지...?

 

728x90
반응형