코딩공부/프로그래머스

※[프로그래머스]Lv.1 성격 유형 검사하기 C#

usingsystem 2023. 6. 14. 15:22
728x90

 

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

소스코드1

처음에 똑같은 선택지가 나올 수 도 있다는 걸 생각하지 않아서    values[j, k] += choice;를    values[j, k] = choice;로 만들었었다.

그 결과 계속 오류가 났고 찾는데 시간이 오래 걸리고 답답했다.

using System;

public class Solution
{
    public string solution(string[] survey, int[] choices)
    {
        string answer = "";
        string[,] keys = new string[4, 2]
        {
                {"R", "T" }, {"C", "F" }, {"J","M"}, {"A","N"}
        };
        int[,] values = new int[4, 2];

        for (int i = 0; i < survey.Length; i++)
        {
            string sSurvey = "";
            int choice = 0;

            if (choices[i] > 4)
            {
                sSurvey = survey[i][1].ToString();
                choice = (-1) * (4 - choices[i]);
            }
            else
            {
                sSurvey = survey[i][0].ToString();
                choice = 4 - choices[i];
            }
            for (int j = 0; j < 4; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    if (keys[j, k] == sSurvey)
                    {
                        values[j, k] += choice;
                    }
                }
            }
        }

        for (int i = 0; i < 4; i++)
        {
            if (values[i, 0] >= values[i, 1])
                answer += keys[i, 0];
            else if (values[i, 0] < values[i, 1])
                answer += keys[i, 1];
        }
        return answer;
    }
}

소스코드2

using System;
using System.Collections.Generic;
public class Solution
{
    public string solution(string[] survey, int[] choices)
    {
        string answer = "";

        Dictionary<string, int> dic = new Dictionary<string, int>()
            {
                {"R",0 }, {"T",0 },
                {"C",0 }, {"F",0 },
                {"J",0 }, {"M",0 },
                {"A",0 }, {"N",0 },
            };

        string sSurvey = "";
        int choice = 0;
        for (int i = 0; i < survey.Length; i++)
        {
            if (choices[i] > 4)
            {
                sSurvey = survey[i][1].ToString();
                choice = (-1) * (4 - choices[i]);
            }
            else
            {
                sSurvey = survey[i][0].ToString();
                choice = 4 - choices[i];
            }

            dic[sSurvey] += choice;
        }

        answer += dic["R"] >= dic["T"] ? "R" : "T";
        answer += dic["C"] >= dic["F"] ? "C" : "F";
        answer += dic["J"] >= dic["M"] ? "J" : "M";
        answer += dic["A"] >= dic["N"] ? "A" : "N";

        return answer;
    }
}
728x90