[Programmers] 성격 유형 검사하기

ssu_hyun·2022년 10월 1일
0

Data Structure & Algorithm

목록 보기
64/67
using System;
using System.Text;
using System.Collections.Generic;

public class Solution
{
    public string solution(string[] survey, int[] choices)
    {
        string answer = "";
        // 결과 저장할 Dictionary 초기화
        Dictionary<string, int> result = new Dictionary<string, int>()
        {
                { "R", 0 },
                { "T", 0 },
                { "C", 0 },
                { "F", 0 },
                { "J", 0 },
                { "M", 0 },
                { "A", 0 },
                { "N" , 0 }
        };

        // 1. 점수 매기기

        // for문을 돌려 모든 survey 조사
        for (int i = 0; i < survey.Length; i++)
        {
            
            string type = "";  // 선택된 성격 유형 알파벳 저장할 변수
            string s1 = survey[i];
             
            // 매우 비동의 ~ 약간 비동의, 앞 성격 유형 알파벳에 점수 부여
            if (choices[i] < 4)
            {
                type += s1[0];
                if (choices[i] == 1) result[type] += 3;
                else if (choices[i] == 2) result[type] += 2;
                else if (choices[i] == 3) result[type] += 1;
            }
            // 약간 동의 ~ 매우 동의, 뒤 성격 유형 알파벳에 점수 부여
            else if (choices[i] > 4)
            {
                type += s1[1];
                if (choices[i] == 5) result[type] += 1;
                else if (choices[i] == 6) result[type] += 2;
                else if (choices[i] == 7) result[type] += 3;
            }
        }

        // 2. 결과값 내기
        List<string> s = new List<string>() { "R", "T", "C", "F", "J", "M", "A", "N" };

        // for문으로 성격 유형 2개씩 검사
        for (int j = 0; j < s.Count; j += 2)
        {
            string c1 = s[j];
            string c2 = s[j + 1];

            // 각 지표의 결과값 비교
            if (result[c1] < result[c2]) answer += s[j + 1];
            else if (result[c1] > result[c2]) answer += s[j];
            // 결과값 같을 경우 사전순으로 빠른 성격 유형 알파벳 출력하도록 바이트 비교
            else if (result[c1] == result[c2])
            {
                byte[] b1 = Encoding.ASCII.GetBytes(c1);
                byte[] b2 = Encoding.ASCII.GetBytes(c2);
                answer += (b1[0] < b2[0]) ? c1 : c2;
            }
        }
        return answer;
    }
}

0개의 댓글