문제 링크
풀이 방법
- 4점을 기준으로 survey에 두 유형 중 어떤 유형에 점수를 더할지 결정
- 미리 vector에 점수,성격 유형을 저장하고 해당 유형에 점수를 더 함
풀이 개선
- map을 사용했더라면 조금 더 간결한 코드가 나왔을 것 같다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<pair<pair<char, int>, pair<char, int>>> mbti{ {{'R',0}, {'T',0} },{{'C',0}, {'F',0} },{{'J',0}, {'M',0} },{{'A',0}, {'N',0} } };
vector<int> score{ 3,2,1,0,1,2,3 };
string solution(vector<string> survey, vector<int> choices) {
string answer = "";
for (int i = 0; i < choices.size(); i++)
{
if (choices[i] > 4)
{
for_each(mbti.begin(), mbti.end(), [&](pair<pair<char, int>, pair<char, int>>& a) {
if (survey[i][1] == a.first.first)
{
a.first.second += score[choices[i] - 1];
}
else if (survey[i][1] == a.second.first)
{
a.second.second += score[choices[i] - 1];
}
});
}
else if (choices[i] < 4)
{
for_each(mbti.begin(), mbti.end(), [&](pair<pair<char, int>, pair<char, int>>& a) {
if (survey[i][0] == a.first.first)
{
a.first.second += score[choices[i] - 1];
}
else if (survey[i][0] == a.second.first)
{
a.second.second += score[choices[i] - 1];
}
});
}
}
for (const auto& pType : mbti)
{
if (pType.first.second != pType.second.second)
{
if (pType.first.second > pType.second.second)
{
answer += pType.first.first;
}
else
{
answer += pType.second.first;
}
}
else
{
answer += pType.first.first;
}
}
return answer;
}