프로그래머스 성격 유형 검사하기 문제풀이 in C++
문제 URL
https://school.programmers.co.kr/learn/courses/30/lessons/118666
문제 요구사항
- 성격 유형 검사는 다음과 같은 4개 지표로 성격 유형을 구분합니다. 성격은 각 지표에서 두 유형 중 하나로 결정됩니다.
- 검사지에는 총 n개의 질문이 있고, 각 질문에는 아래와 같은 7개의 선택지가 있습니다.
- 질문마다 판단하는 지표를 담은 1차원 문자열 배열 survey와 검사자가 각 질문마다 선택한 선택지를 담은 1차원 정수 배열 choices가 매개변수로 주어집니다.
- 1 ≤ survey의 길이 ( = n) ≤ 1,000
- survey의 원소는 “RT”, “TR”, “FC”, “CF”, “MJ”, “JM”, “AN”, “NA” 중 하나입니다.
- choices의 길이 = survey의 길이
접근 방법
- 가장 기초적인 구현 방법으로 문제에 접근했다.
풀이 순서
- survey의 각 인덱스에 접근하여, 해당 survey에서의 점수를 각각 체크 한다.
- survey에서 작성한 mbti 유형에 맞는 total count 점수에 갱신한다.
- 이와 같은 작업을 반복하여 각각의 유형의 total 점수를 구한다.
- 각 점수를 비교하여 큰값을 mbti 유형으로 하되, 0점이면 알파벳 순으로 한다.
- 성격 유형 반환
소스코드
#include <string>
#include <vector>
using namespace std;
int scoreA[] = {0, 3, 2, 1};
int scoreB[] = {0, 1, 2, 3};
vector<int> calc_score(string mbti, int choice) {
int a = 0, b = 0;
if(mbti[0] == 'R' || mbti[0] == 'C' || mbti[0] == 'J' || mbti[0] == 'A') {
if(choice <= 3) {
a = scoreA[choice];
}else {
b = scoreB[choice - 4];
}
} else {
if(choice <= 3) {
b = scoreA[choice];
}else {
a = scoreB[choice - 4];
}
}
return {a, b};
}
string solution(vector<string> survey, vector<int> choices) {
string answer = "";
vector<int> total_first(2, 0);
vector<int> total_second(2, 0);
vector<int> total_third(2, 0);
vector<int> total_four(2, 0);
for(int i = 0; i < choices.size(); i ++) {
vector<int> score = calc_score(survey[i], choices[i]);
if(survey[i].compare("RT") == 0 || survey[i].compare("TR") == 0) {
total_first[0] += score[0];
total_first[1] += score[1];
} else if(survey[i].compare("CF") == 0 || survey[i].compare("FC") == 0) {
total_second[0] += score[0];
total_second[1] += score[1];
} else if(survey[i].compare("JM") == 0 || survey[i].compare("MJ") == 0) {
total_third[0] += score[0];
total_third[1] += score[1];
} else {
total_four[0] += score[0];
total_four[1] += score[1];
}
}
if(total_first[0] < total_first[1]) {
answer += "T";
} else {
answer += "R";
}
if(total_second[0] < total_second[1]) {
answer += "F";
} else {
answer += "C";
}
if(total_third[0] < total_third[1]) {
answer += "M";
}else {
answer += "J";
}
if(total_four[0] < total_four[1]) {
answer += "N";
} else {
answer += "A";
}
return answer;
}