728x90
SMALL

using System;

public class Solution {
    public string solution(string[] survey, int[] choices) {
        string answer = "";
        int[] type = new int[8];
        // R T C F J M A N
        string[] types = new string[8] {"R", "T", "C", "F", "J", "M", "A", "N"};
        
        for(int i = 0;i<survey.Length;i++)
        {
            if((survey[i])[0] == 'A')
            {
                if(choices[i] >= 4)
                {
                    type[7] += choices[i]-4;
                }
                else
                {
                    type[6] += 4-choices[i];
                }
            }
            else if((survey[i])[0] == 'N')
            {
                if(choices[i] >= 4)
                {
                    type[6] += choices[i]-4;
                }
                else
                {
                    type[7] += 4-choices[i];
                }
            }
            else if((survey[i])[0] == 'C')
            {
                if(choices[i] >= 4)
                {
                    type[3] += choices[i]-4;
                }
                else
                {
                    type[2] += 4-choices[i];
                }
            }
            else if((survey[i])[0] == 'F')
            {
                if(choices[i] >= 4)
                {
                    type[2] += choices[i]-4;
                }
                else
                {
                    type[3] += 4-choices[i];
                }
            }
            else if((survey[i])[0] == 'M')
            {
                if(choices[i] >= 4)
                {
                    type[4] += choices[i]-4;
                }
                else
                {
                    type[5] += 4-choices[i];
                }
            }
            else if((survey[i])[0] == 'J')
            {
                if(choices[i] >= 4)
                {
                    type[5] += choices[i]-4;
                }
                else
                {
                    type[4] += 4-choices[i];
                }
            }
            else if((survey[i])[0] == 'R')
            {
                if(choices[i] >= 4)
                {
                    type[1] += choices[i]-4;
                }
                else
                {
                    type[0] += 4-choices[i];
                }
            }
            else if((survey[i])[0] == 'T')
            {
                if(choices[i] >= 4)
                {
                    type[0] += choices[i]-4;
                }
                else
                {
                    type[1] += 4-choices[i];
                }
            }
        }
        
        for(int i = 0;i<4;i++)
        {
            if(type[2*i] >= type[2*i+1])
            {
                answer += types[2*i];
            }
            else
            {
                answer += types[2*i+1];
            }
        }

        return answer;
    }
}

 

 

/// 완전 노가다로 풀었다.

 

딕셔너리를 활용하는 거였다.

 

using System;
using System.Collections.Generic;

public class Solution {
    public string solution(string[] survey, int[] choices) {

        Dictionary<string, int> result = new Dictionary<string, int>
        {
            {"R",0}, {"T",0}, {"C",0}, {"F",0}, {"J",0}, {"M",0}, {"A",0}, {"N",0}
        };

        for (int i=0; i<survey.Length; i++)
        {               
            int values=0;
            string valueName="";

            if (choices[i] >= 4)
            {
                values = 4-choices[i];
                valueName=survey[i][0].ToString();
            }
            else
            {
                values = choices[i]-4;
                valueName=survey[i][1].ToString();
            }

            result[valueName] += values;
        }
        return MBTI(result);
    }

    public string MBTI(Dictionary<string, int> result)
    {
        string answer = result["R"] < result["T"] ? "T" : "R";
        answer += result["C"] < result["F"] ? "F" : "C";
        answer += result["J"] < result["M"] ? "M" : "J";
        answer += result["A"] < result["N"] ? "N" : "A";
        // 점수가 낮을 수록(마이너스 절대값이 클 수록) 선택되도록 설계
        return answer;        
    }
}

 

 

 

파이썬

 

 

def solution(survey, choices):

    my_dict = {"RT":0,"CF":0,"JM":0,"AN":0}
    for A,B in zip(survey,choices):
        if A not in my_dict.keys():
            A = A[::-1]
            my_dict[A] -= B-4
        else:
            my_dict[A] += B-4

    result = ""
    for name in my_dict.keys():
        if my_dict[name] > 0:
            result += name[1]
        elif my_dict[name] < 0:
            result += name[0]
        else:
            result += sorted(name)[0]

    return result

 

자바

//////

import java.util.HashMap;

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        HashMap<Character, Integer> map = new HashMap<>();
        map.put('R', 0);map.put('T', 0);
        map.put('C', 0);map.put('F', 0);
        map.put('J', 0);map.put('M', 0);
        map.put('A', 0);map.put('N', 0);

        for (int i = 0; i < survey.length; i++) {
            if (choices[i] > 4)
                map.put(survey[i].charAt(1), map.get(survey[i].charAt(1)) + choices[i] - 4);
            else if (choices[i] < 4) {
                map.put(survey[i].charAt(0), map.get(survey[i].charAt(0)) + 4 - choices[i]);
            }
        }


        if (map.get('R') >= map.get('T'))
            answer = "R";
        else
            answer = "T";

        if (map.get('C') >= map.get('F'))
            answer += "C";
        else
            answer += "F";

        if (map.get('J') >= map.get('M'))
            answer += "J";
        else
            answer += "M";

        if (map.get('A') >= map.get('N'))
            answer += "A";
        else
            answer += "N";

        return answer;
    }
}

 

 

import java.util.HashMap;

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        char [][] type = {{'R', 'T'}, {'C', 'F'}, {'J', 'M'}, {'A', 'N'}};
        int [] score = {0, 3, 2, 1, 0, 1, 2, 3};
        HashMap<Character, Integer> point = new HashMap<Character, Integer>();

        // 점수 기록할 배열 초기화 
        for (char[] t : type) {
            point.put(t[0], 0);
            point.put(t[1], 0);
        }

        // 점수 기록 
        for (int idx = 0; idx < choices.length; idx++){
            if(choices[idx] > 4){
                point.put(survey[idx].charAt(1), point.get(survey[idx].charAt(1)) + score[choices[idx]]);
            } else {
                point.put(survey[idx].charAt(0), point.get(survey[idx].charAt(0)) + score[choices[idx]]);
            }
        }

        // 지표 별 점수 비교 후 유형 기입
        for (char[] t : type) {
            answer += (point.get(t[1]) <= point.get(t[0])) ? t[0] : t[1];
        }

        return answer;
    }
}
728x90

설정

트랙백

댓글