본문 바로가기
Coding Test/프로그래머스

[프로그래머스] Lv.1 성격 유형 검사하기 (java)

by CSEGR 2024. 5. 17.
728x90

 

출처 : https://datalabbit.tistory.com/155

 

 

전체 코드  - 수정 전 
import java.util.*;
class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        int[] score = {3,2,1,0,1,2,3};
        
        // 성격 유형 등록하기 
        char[] mbti_char = {'R', 'T', 'C', 'F', 'J', 'M', 'A','N'};
        
        HashMap<Character, Integer> mbti = new HashMap<>();
        
        for(char c : mbti_char)
        {
            mbti.put(c, 0);
        }
        
        //["AN", "CF", "MJ", "RT", "NA"]
        //[5, 3, 2, 7, 5]
        char type ;
        
        for(int i = 0 ; i< choices.length; i++)
        {
            
            if(choices[i] <= 3){
                type = survey[i].charAt(0);
                mbti.put(type, mbti.get(type)+score[choices[i]-1]);
            }
            else
            {
                type = survey[i].charAt(1);
                mbti.put(type, mbti.get(type)+score[choices[i]-1]);
                
            }
        }
        System.out.println(mbti);
        
        for(int i = 0; i<4; i++)
        {
           if(mbti.get(mbti_char[2*i]) >= mbti.get(mbti_char[2*i+1]))
               answer += mbti_char[2*i];
            else
                answer += mbti_char[2*i+1];
        }
        return answer;
    }
}

 

 

 

 

 

728x90