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

이한솔·2023년 10월 25일
0

프로그래머스_레벨1

목록 보기
61/65
post-thumbnail

✨️ 문제 설명

: 검사 결과는 모든 질문의 성격 유형 점수를 더하여 각 지표에서 더 높은 점수를 받은 성격 유형이 검사자의 성격 유형이라고 판단합니다. 단, 하나의 지표에서 각 성격 유형 점수가 같으면, 두 성격 유형 중 사전 순으로 빠른 성격 유형을 검사자의 성격 유형이라고 판단합니다.

질문마다 판단하는 지표를 담은 1차원 문자열 배열 survey와 검사자가 각 질문마다 선택한 선택지를 담은 1차원 정수 배열 choices가 매개변수로 주어집니다. 이때, 검사자의 성격 유형 검사 결과를 지표 번호 순서대로 return 하도록 solution 함수를 완성해주세요.

 -> 자세한 내용 보러가기

🎲 자바 풀이

import java.util.*;
class Solution {
    public String solution(String[] survey, int[] choices) {
        StringBuilder sb = new StringBuilder();
        
        //Map에 해당 성격 유형과 점수를 넣는다
        Map<String, Integer> map = new HashMap<>();
        map.put("RT", 0);
        map.put("CF", 0);
        map.put("JM", 0);
        map.put("AN", 0);
        //R C J A < 0 <R F M N 
        int[] score = {-3, -2, -1, 0, 1, 2, 3};
        
        for(int idx = 0; idx < survey.length; idx++){
            //성격 유형별로 if문 생성
            if(survey[idx].equals("RT")){
                map.put("RT", map.get("RT") + score[choices[idx] -1]);
            }else if(survey[idx].equals("TR")){
                map.put("RT", map.get("RT") + score[choices[idx] -1] * (-1)); 
            }
            
            if(survey[idx].equals("CF")){
                map.put("CF", map.get("CF") + score[choices[idx] -1]);
            }else if(survey[idx].equals("FC")){
                map.put("CF", map.get("CF") + score[choices[idx] -1] * (-1));
            }
            
            if(survey[idx].equals("JM")){
                map.put("JM", map.get("JM") + score[choices[idx] -1]);
            }else if(survey[idx].equals("MJ")){
                map.put("JM", map.get("JM") + score[choices[idx] -1] * (-1));
            }
            
            if(survey[idx].equals("AN")){
                map.put("AN", map.get("AN") + score[choices[idx] -1]);
            }else if(survey[idx].equals("NA")){
                map.put("AN", map.get("AN") + score[choices[idx] -1] * (-1));
            }
            
        }
        
        sb.append((map.get("RT") <= 0) ? "R" : "T");
        sb.append((map.get("CF") <= 0) ? "C" : "F");
        sb.append((map.get("JM") <= 0) ? "J" : "M");
        sb.append((map.get("AN") <= 0) ? "A" : "N");
        
        return sb.toString();
    }
}

풀이 설명

: 주어진 성격 유형을 map에 담고, 해당하는 문제의 점수를 저장하였다.
: 성격 문항이 반대일 수 도 있으므로 각각의 성격 유형 중 하나는 -로 다른 하나는 +로 상정하고, -1을 곱해 점수를 계산하였다.
profile
개인 공부용

0개의 댓글