프로그래머스

[프로그래머스/Java] 성격 유형 검사하기

웅둘 2023. 7. 12. 20:56

📚성격 유형 검사하기

[JAVA] 프로그래머스 Level 1.

코딩테스트 연습 - 성격 유형 검사하기 | 프로그래머스 스쿨 (programmers.co.kr)

문제

(생략)

제한사항

(생략)

입출력 예

survey  choices result
["AN", "CF", "MJ", "RT", "NA"] [5, 3, 2, 7, 5] "TCMA"
["TR", "RT", "TR"] [7, 1, 3] "RCJA"

문제 해결 방안

R,T, C,F, J,M, A,N을 각각 저장한다

값이 4보다 작을 경우에는 왼쪽 문자에 점수를 올리고, 클 경우에는 오른쪽 문자에 점수를 올린다

계산 후 RT, CF, JM, AN 로 비교한다.

소스 코드

import java.util.*;
class Solution {
    public String solution(String[] survey, int[] choices) {
        StringBuilder answer = new StringBuilder();
        Map<String, 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);
        
        int[] score = {3, 2, 1, 0, 1, 2, 3};
        
        for(int i=0; i<survey.length; i++) {
            String[] strs = survey[i].split("");
            // System.out.println()
            int s = choices[i]-1;
            if(s<4) {
                int x = map.get(strs[0]);
                map.put(strs[0], x+score[s]);
            } else if(choices[i]>4) {
                int x = map.get(strs[1]);
                map.put(strs[1], x+score[s]);                
            }
        }
        
        if(map.get("R") >= map.get("T")) {
            answer.append("R");
        } else{
            answer.append("T");
        }

        if(map.get("C") >= map.get("F")) {
            answer.append("C");
        } else{
            answer.append("F");
        }
        
        if(map.get("J") >= map.get("M")) {
            answer.append("J");
        } else{
            answer.append("M");
        }
        
        if(map.get("A") >= map.get("N")) {
            answer.append("A");
        } else{
            answer.append("N");
        }        
        return answer.toString();
    }
}