프로그래머스 문제 - 성격 유형 검사하기

JUNWOO KIM·2024년 2월 10일
0

알고리즘 풀이

목록 보기
81/105

프로그래머스 성격 유형 검사하기 문제 풀이를 진행하였습니다.

문제 해석

아래와 같은 문제가 주어집니다.


문제 풀이

요즘 사람들이 하는 MBTI검사와 비슷한 문제입니다.
숫자를 보고 4보다 작으면 주어진 두 알파벳 중 첫번째 알파벳의 유형 점수가 올라가며, 4보다 클 경우 두번째 알파벳의 유형 점수가 올라갑니다.
모든 검사를 마치고 나온 점수를 비교하며 답을 도출해내면 됩니다.

전체 코드

#include <bits/stdc++.h>
#include <string>
#include <vector>

using namespace std;

string solution(vector<string> survey, vector<int> choices) {
    string answer = "";
    vector<int> result(4, 0);
    
    //값 계산
    for(int i = 0; i < survey.size(); i++)
    {
        if(survey[i][0] == 'R' || survey[i][0] == 'T')
        {
            if(choices[i] < 4){
                if(survey[i][0] == 'R')
                    result[0] -= 4 - choices[i];
                else
                    result[0] += 4 - choices[i];
            }else if(choices[i] > 4){
                if(survey[i][0] == 'R')
                    result[0] += choices[i] - 4;
                else
                    result[0] -= choices[i] - 4;
            }
        }
        else if(survey[i][0] == 'C' || survey[i][0] == 'F')
        {
            if(choices[i] < 4){
                if(survey[i][0] == 'C')
                    result[1] -= 4 - choices[i];
                else
                    result[1] += 4 - choices[i];
            }else if(choices[i] > 4){
                if(survey[i][0] == 'C')
                    result[1] += choices[i] - 4;
                else
                    result[1] -= choices[i] - 4;
            }
        }
        else if(survey[i][0] == 'J' || survey[i][0] == 'M')
        {
            if(choices[i] < 4){
                if(survey[i][0] == 'J')
                    result[2] -= 4 - choices[i];
                else
                    result[2] += 4 - choices[i];
            }else if(choices[i] > 4){
                if(survey[i][0] == 'J')
                    result[2] += choices[i] - 4;
                else
                    result[2] -= choices[i] - 4;
            }
        }
        else if(survey[i][0] == 'A' || survey[i][0] == 'N')
        {
            if(choices[i] < 4){
                if(survey[i][0] == 'A')
                    result[3] -= 4 - choices[i];
                else
                    result[3] += 4 - choices[i];
            }else if(choices[i] > 4){
                if(survey[i][0] == 'A')
                    result[3] += choices[i] - 4;
                else
                    result[3] -= choices[i] - 4;
            }
        }
    }
    //값에 맞는 알파벳 입력
    for(int i = 0; i < 4; i++)
    {
        if(i == 0)
            if(result[i] <= 0)
                answer += 'R';
            else
                answer += 'T';
        else if(i == 1)
            if(result[i] <= 0)
                answer += 'C';
            else
                answer += 'F';
        else if(i == 2)
            if(result[i] <= 0)
                answer += 'J';
            else
                answer += 'M';
        else
            if(result[i] <= 0)
                answer += 'A';
            else
                answer += 'N';
    }
    
    return answer;
}

출저

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

profile
게임 프로그래머 준비생

0개의 댓글