https://school.programmers.co.kr/learn/courses/30/lessons/118666
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <map>
using namespace std;
string solution(vector<string> survey, vector<int> choices) {
string answer = "";
map<char,int> result;
result.insert({'R',0});
result.insert({'T',0});
result.insert({'C',0});
result.insert({'F',0});
result.insert({'J',0});
result.insert({'M',0});
result.insert({'A',0});
result.insert({'N',0});
for(int i=0;i<survey.size();i++)
{
// 비동의쪽 선택 , 1번유형
if(choices[i]<=3)
{
result[survey[i][0]]+=4-choices[i];
}
// 동의쪽 선택, 2번유형
else if(choices[i]>=5)
{
result[survey[i][1]]+=choices[i]-4;
}
}
if(result['R']<result['T'])
answer+="T";
else
answer+="R";
if(result['C']<result['F'])
answer+='F';
else
answer+='C';
if(result['J']<result['M'])
answer+='M';
else
answer+='J';
if(result['A']<result['N'])
answer+='N';
else
answer+='A';
return answer;
}
mbti같이 성격유형검사를 한 결과를 리턴하는 문제이다.
survey에는 어떤 지표의 유형문제인지를, choices에는 1~7중 어떤 답을 선택했는지를 나타낸다.
map을 이용하여 쉽게 구현할 수 있었다.
map<char,int> result;
result.insert({'R',0});
result.insert({'T',0});
result.insert({'C',0});
result.insert({'F',0});
result.insert({'J',0});
result.insert({'M',0});
result.insert({'A',0});
result.insert({'N',0});
8개의 유형을 각각 키값으로하는 map을 만들었다.
for(int i=0;i<survey.size();i++)
{
// 비동의쪽 선택 , 1번유형
if(choices[i]<=3)
{
result[survey[i][0]]+=4-choices[i];
}
// 동의쪽 선택, 2번유형
else if(choices[i]>=5)
{
result[survey[i][1]]+=choices[i]-4;
}
}
그리고 for문에서 몇번을 선택했는지를 if문으로 검사하여 비동의쪽이면 survey[i]의 첫번째 유형쪽 점수를 얻게되므로
survey[i][0]을 키로하는 곳에 점수를 더한다.
반대로 동의쪽이면 survey[i][1]을 키로하는 곳에 점수를 더한다.
if(result['R']<result['T'])
answer+="T";
else
answer+="R";
if(result['C']<result['F'])
answer+='F';
else
answer+='C';
if(result['J']<result['M'])
answer+='M';
else
answer+='J';
if(result['A']<result['N'])
answer+='N';
else
answer+='A';
마지막으로 두 개의 유형중 어느쪽이 점수가 더 높나를 if문으로 판단후 answer에 추가하면 된다.
'알고리즘 > 프로그래머스 1단계' 카테고리의 다른 글
프로그래머스 - 신고결과 받기 - C++ (0) | 2024.07.26 |
---|---|
프로그래머스 - 달리기 경주 - C++ (0) | 2024.07.26 |
프로그래머스 - 키패드 누르기 - C++ (0) | 2024.07.25 |
프로그래머스 - 크레인 인형뽑기 게임 - C++ (0) | 2024.07.25 |
프로그래머스 - 체육복 - C++ (0) | 2024.07.25 |