일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- springSecurity
- Spring boot
- java
- Linux
- sql
- css
- programmers
- 반복문
- codingtest
- 코딩테스트
- 싱글톤
- javascript
- html
- gradle
- 시큐리티 로그아웃
- 시큐리티로그인
- security
- 스프링 부트
- StyleSheet
- 2차원배열
- 시큐리티 로그인
- 시큐리티
- 리눅스
- springboot
- 목록
- 프로그래머스
- JAVA11
- 로그인
- 소스트리
- input태그
Archives
- Today
- Total
JAVAIARY
프로그래머스) 성격유형 검사하기 본문
문제: https://school.programmers.co.kr/learn/courses/30/lessons/118666
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
class Solution {
public String solution(String[] survey, int[] choices) {
String answer = "";
// 비동의 - 동의 선택지
//RT, CF, MJ, AN
// 동점인 경우 알파벳순
int[] personality = new int[8];
// 각각 R, T, C, F, J, M, A, N 의 점수를 담을 배열
for (int i = 0; i < survey.length; i++) {
// 비동의 선택지 / 동의 선택지
int disagree = applyScore(survey[i].charAt(0));
int agree = applyScore(survey[i].charAt(1));
// 4 : 모르겠음 선택시 점수 변동이 없으므로 skip
switch (choices[i]) {
case 1:
personality[disagree] +=3;
break;
case 2:
personality[disagree] +=2;
break;
case 3:
personality[disagree] +=1;
break;
case 5:
personality[agree] +=1;
break;
case 6:
personality[agree] +=2;
break;
case 7:
personality[agree] +=3;
break;
}
}
answer = personality[0]>=personality[1] ? answer+'R': answer+'T';
answer = personality[2]>=personality[3] ? answer+'C': answer+'F';
answer = personality[4]>=personality[5] ? answer+'J': answer+'M';
answer = personality[6]>=personality[7] ? answer+'A': answer+'N';
return answer;
}
// 점수를 더해줄 인덱스 구하는 함수
public static int applyScore(char s) {
int idx = 0;
switch (s) {
case 'R':
idx = 0;
return idx;
case 'T':
idx = 1;
return idx;
case 'C':
idx = 2;
return idx;
case 'F':
idx = 3;
return idx;
case 'J':
idx = 4;
return idx;
case 'M':
idx = 5;
return idx;
case 'A':
idx = 6;
return idx;
case 'N':
idx = 7;
return idx;
}
return idx;
}
}
- 그냥 문제 그대로 구현해버렸음
- 다른사람의 풀이를 보니 점수를 한번에 구현하는 게 핵심이었던 것 같다.
import java.util.HashMap;
class Solution {
public String solution(String[] survey, int[] choices) {
String answer = "";
char [][] type = {{'R', 'T'}, {'C', 'F'}, {'J', 'M'}, {'A', 'N'}};
int [] score = {0, 3, 2, 1, 0, 1, 2, 3};
HashMap<Character, Integer> point = new HashMap<Character, Integer>();
// 점수 기록할 배열 초기화
for (char[] t : type) {
point.put(t[0], 0);
point.put(t[1], 0);
}
// 점수 기록
for (int idx = 0; idx < choices.length; idx++){
if(choices[idx] > 4){
point.put(survey[idx].charAt(1), point.get(survey[idx].charAt(1)) + score[choices[idx]]);
} else {
point.put(survey[idx].charAt(0), point.get(survey[idx].charAt(0)) + score[choices[idx]]);
}
}
// 지표 별 점수 비교 후 유형 기입
for (char[] t : type) {
answer += (point.get(t[1]) <= point.get(t[0])) ? t[0] : t[1];
}
return answer;
}
}
- 안쓰는 자료형은 까먹게 된다. (ex. hashMap)
- key, value 가 쌍으로 있어 유형별 점수를 부여해 주기에 딱인 것 같다.
'examplePractice' 카테고리의 다른 글
프로그래머스 ) n^2 배열 자르기 (0) | 2023.04.03 |
---|---|
프로그래머스) 크레인 인형뽑기 게임 (0) | 2023.03.22 |
프로그래머스) OX 퀴즈 (0) | 2023.03.21 |
프로그래머스) H-Index (0) | 2023.03.04 |
프로그래머스) 약수 구하기 (0) | 2023.03.01 |