본문 바로가기
Java/Programmers

[Lv.1]성격 유형 검사하기

by GLOWWW 2023. 2. 1.

이 문제는 잘 풀었다고 생각했지만, 문제를 잘못 이해하여 시간이 오래걸렸다.

같은 성격 유형이 또 나온다면 더 높은 점수로 최신화 시켜줘야하는것으로 이해를 하고 풀었었다.

아무리봐도 고칠것이 없었고, 다른 사람이 한 풀이를 살펴보았다. 오류는 더 높은 점수로 최신화가 아니라 점수를 누적시켜줘야 하는 것 이었다.

아래의 코드는 모든 테스트를 통과한 코드이고, 그 아래의 코드는 잘못이해하여 풀었던 코드이다.

package Lv_1;

import java.util.HashMap;
import java.util.Map;

public class 성격유형검사하기 {
    public static String solution(String[] survey, int[] choices) {
        String answer = "";
        Map<String, Integer> type_score = new HashMap<>();
        type_score.put("R", 0);
        type_score.put("T", 0);
        type_score.put("C", 0);
        type_score.put("F", 0);
        type_score.put("J", 0);
        type_score.put("M", 0);
        type_score.put("A", 0);
        type_score.put("N", 0);
        for (int i = 0; i < survey.length; i++) {
            String[] type = survey[i].split("");
            if (choices[i] == 1) {
                type_score.put(type[0], type_score.get(type[0]) + 3);
            }
            if (choices[i] == 2) {
                type_score.put(type[0], type_score.get(type[0]) + 2);
            }
            if (choices[i] == 3) {
                type_score.put(type[0], type_score.get(type[0]) + 1);
            }
            if (choices[i] == 4) {
                continue;
            }
            if (choices[i] == 5) {
                type_score.put(type[1], type_score.get(type[1]) + 1);
            }
            if (choices[i] == 6) {
                type_score.put(type[1], type_score.get(type[1]) + 2);
            }
            if (choices[i] == 7) {
                type_score.put(type[1], type_score.get(type[1]) + 3);
            }
        }
        if (type_score.get("R") >= type_score.get("T")) {
            answer += "R";
        } else {
            answer += "T";
        }
        if (type_score.get("C") >= type_score.get("F")) {
            answer += "C";
        } else {
            answer += "F";
        }
        if (type_score.get("J") >= type_score.get("M")) {
            answer += "J";
        } else {
            answer += "M";
        }
        if (type_score.get("A") >= type_score.get("N")) {
            answer += "A";
        } else {
            answer += "N";
        }
        return answer;
    }

    public static void main(String[] args) {
        String[] survey = {"AN", "CF", "MJ", "RT", "NA"};
        int[] choices = {5, 3, 2, 7, 5};
        String result = solution(survey, choices);
        System.out.println(result);
    }
}

잘못풀이한 코드

package Lv_1;

import java.util.HashMap;
import java.util.Map;

public class 성격유형검사하기 {
    public static String solution(String[] survey, int[] choices) {
        String answer = "";
        Map<String, Integer> type_score = new HashMap<>();
        type_score.put("R", 0);
        type_score.put("T", 0);
        type_score.put("C", 0);
        type_score.put("F", 0);
        type_score.put("J", 0);
        type_score.put("M", 0);
        type_score.put("A", 0);
        type_score.put("N", 0);
        for (int i = 0; i < survey.length; i++) {
            String[] type = survey[i].split("");
            if (choices[i] == 1) {
                if (type_score.get(type[0]) > 3) {
                    continue;
                } else {
                    type_score.put(type[0], 3);
                }
            }
            if (choices[i] == 2) {
                if (type_score.get(type[0]) > 2) {
                    continue;
                } else {
                    type_score.put(type[0], 2);
                }
            }
            if (choices[i] == 3) {
                if (type_score.get(type[0]) > 1) {
                    continue;
                } else {
                    type_score.put(type[0], 1);
                }
            }
            if (choices[i] == 4) {
                continue;
            }
            if (choices[i] == 5) {
                if (type_score.get(type[1]) > 1) {
                    continue;
                } else {
                    type_score.put(type[1], 1);
                }
            }
            if (choices[i] == 6) {
                if (type_score.get(type[1]) > 2) {
                    continue;
                } else {
                    type_score.put(type[1], 2);
                }
            }
            if (choices[i] == 7) {
                if (type_score.get(type[1]) > 3) {
                    continue;
                } else {
                    type_score.put(type[1], 3);
                }
            }
        }
        if (type_score.get("R") >= type_score.get("T")) {
            answer += "R";
        } else {
            answer += "T";
        }
        if (type_score.get("C") >= type_score.get("F")) {
            answer += "C";
        } else {
            answer += "F";
        }
        if (type_score.get("J") >= type_score.get("M")) {
            answer += "J";
        } else {
            answer += "M";
        }
        if (type_score.get("A") >= type_score.get("N")) {
            answer += "A";
        } else {
            answer += "N";
        }
        return answer;
    }

    public static void main(String[] args) {
        String[] survey = {"AN", "CF", "MJ", "RT", "NA"};
        int[] choices = {5, 3, 2, 7, 5};
        String result = solution(survey, choices);
        System.out.println(result);
    }
}

틀렸다면 문제를 잘 못 이해한것은 아닌지 다시 생각해보자!

'Java > Programmers' 카테고리의 다른 글

[Lv.1]개인정보 수집 유효기간  (0) 2023.02.02
[Lv.1]햄버거 만들기  (0) 2023.02.01
[Lv.1]문자열 나누기  (0) 2023.01.31
[Lv.1]크레인 인형뽑기 게임  (0) 2023.01.30
[Lv.1]키패드 누르기  (0) 2023.01.30

댓글