프로그래머스 Lv.1 - 성격 유형 검사하기

2024.02.05 (MON)


문제 : 성격 유형 검사하기


풀이


결과 코드

/*
 * Date : 2024.02.05 (MON)
 * Title : 프로그래머스 Lv.1 - 성격 유형 검사하기
 * Link : https://school.programmers.co.kr/learn/courses/30/lessons/118666
*/

import java.util.*;

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        int index = 0;
        char[] words = new char[8];
        int[] counts = new int[8];
        
        words[0] = 'R';
        words[1] = 'T';
        words[2] = 'C';
        words[3] = 'F';
        words[4] = 'J';
        words[5] = 'M';
        words[6] = 'A';
        words[7] = 'N';
        
        for (int i = 0; i < counts.length; i++) {
            counts[i] = 0;
        }
        
        // 점수 매기기
        while (index < survey.length) {
            if (choices[index] > 4) { // 동의
                for (int i = 0; i < words.length; i++) {
                    if (survey[index].charAt(1) == words[i]) {
                        counts[i] += choices[index] - 4;
                    }
                }
            }
            else if (choices[index] < 4) { // 비동의
                for (int i = 0; i < words.length; i++) {
                    if (survey[index].charAt(0) == words[i]) {
                        counts[i] += 4 - choices[index];
                    }
                }
            }
            
            index++;
        }
        
        // 유형 결정
        if (counts[0] >= counts[1]) {
            answer += "R";
        }
        else {
            answer += "T";
        }
        
        if (counts[2] >= counts[3]) {
            answer += "C";
        }
        else {
            answer += "F";
        }
        
        if (counts[4] >= counts[5]) {
            answer += "J";
        }
        else {
            answer += "M";
        }
        
        if (counts[6] >= counts[7]) {
            answer += "A";
        }
        else {
            answer += "N";
        }
        
        return answer;
    }
}