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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

지문이 긴 관계로 링크만 첨부한다..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
 
public class Solution
{
    //MBTI별 점수가 담긴 Dictionary
    public Dictionary<charint> MBTIPointDic = new Dictionary<charint>();
 
    //점수 계산표
    private int[] Point = { 3210123 };
 
    public string solution(string[] survey, int[] choices)
    {
        //MBTI표 초기화
        string[] MBTIs = { "RT""CF""JM""AN" };
 
        char[] resultMBTI = new char[4];
 
        //설문대로 MBTI별 점수 계산
        for (int i = 0; i < survey.Length; i++)
            CalcPoint(survey[i][0], survey[i][1], choices[i]);
 
        //계산된 MBTI별 점수를 바탕으로 최종 MBTI 판단
        for (int i = 0; i < MBTIs.Length; i++)
        {
            char mbti = SelectMBTI(MBTIs[i][0], MBTIs[i][1]);
            resultMBTI[i] = mbti;
        }
 
        return new string(resultMBTI);
    }
 
    private void CalcPoint(char negative, char positive, int choice)
    {
        if (!MBTIPointDic.ContainsKey(negative))
            MBTIPointDic[negative] = 0;
 
        if (!MBTIPointDic.ContainsKey(positive))
            MBTIPointDic[positive] = 0;
 
        //'모르겠음' 선택지라면 점수 계산 필요 없음
        if (choice == 4)
            return;
 
        char mbti = choice <= 4 ? negative : positive;
 
        MBTIPointDic[mbti] += Point[choice - 1];
    }
 
    private char SelectMBTI(char a, char b)
    {
        if (!MBTIPointDic.ContainsKey(a))
            MBTIPointDic[a] = 0;
 
        if (!MBTIPointDic.ContainsKey(b))
            MBTIPointDic[b] = 0;
 
        //점수가 같다면 사전순이 빠른 MBTI를 선택
        if (MBTIPointDic[a] == MBTIPointDic[b])
            return a < b ? a : b;
 
        //점수가 높은 MBTI를 선택
        return MBTIPointDic[a] > MBTIPointDic[b] ? a : b;
    }
}
cs

+ Recent posts