문제 풀기/C#

69. 성격 유형 검사하기

kagan-draca 2025. 2. 19. 14:48

 

기본 틀 :

using System;

public class Solution {
    public string solution(string[] survey, int[] choices) {
        string answer = "";
        return answer;
    }
}

 

문제가 엄청 길지만 사실은 별거 아닌 문제다...

 

Dictionary를 이용해 성격 유형 단어(char)를 key, 0으로 value를 결정해준다.

 

매개변수 choices를 반복문으로 순회하면서

 

choices[index] 값이 4이면 continue로 다음 반복문으로 넘어가고

 

그 밖에

 

1 ~ 3이면 Dictionary에 4 - choices[index] 값을

5 ~ 7이면 Dictionary에 choices[index] - 4;

 

를 해준다.

 

이때, key는 매개변수로 주어진 survey를 

 

survey[index].ToCharArray()로 char 형으로 만들어

 

해당 배열의 값을 사용하면 된다.

 

마지막으로 Dictionary에 있는 key에 따른 value를 비교해

 

어떤 성격 유형인지 문자열에 담아주기만 하면 된다.

 

using System;
using System.Collections.Generic;

public class Solution {
    public string solution(string[] survey, int[] choices) {
        Dictionary<char, int> dict = new Dictionary<char, int>();
        
        dict['R'] = 0;
        dict['T'] = 0;
        dict['C'] = 0;
        dict['F'] = 0;
        dict['J'] = 0;
        dict['M'] = 0;
        dict['A'] = 0;
        dict['N'] = 0;
        
        for(int i = 0; i < choices.Length; i++)
        {
            if(choices[i] == 4) continue;
            
            char[] key = survey[i].ToCharArray();
            
            switch(choices[i])
            {
                case 1:
                case 2:
                case 3:
                    dict[key[0]] += 4 - choices[i];
                    break;
                case 5:
                case 6:
                case 7:
                    dict[key[1]] += choices[i] - 4;
                    break;
            }
        }
        
        string personality = dict['R'] == dict['T'] || dict['R'] > dict['T'] ? "R" : "T";
        
        personality += dict['C'] - dict['F'] >= 0 ? "C" : "F";
        personality += dict['J'] - dict['M'] >= 0 ? "J" : "M";
        personality += dict['A'] - dict['N'] >= 0 ? "A" : "N";
        
        return personality;
    }
}

'문제 풀기 > C#' 카테고리의 다른 글

71. 개인정보 수집 유효기간  (0) 2025.02.21
70. 바탕화면 정리  (0) 2025.02.19
68. 햄버거 만들기 (SequenceEqual)  (0) 2025.02.19
67. 둘만의 암호  (0) 2025.02.18
66. 대충 만든 자판(Dictionary)  (0) 2025.02.18