프로그래머스 로또의 최고 순위와 최저 순위: 지워진 번호 세기
2021 Dev-Matching 웹 백엔드 개발자 상반기 문제 풀이입니다.
확정된 번호와 지워진 번호를 나눕니다
0은 번호가 지워져 알 수 없다는 뜻입니다. 먼저 0의 개수를 cntZero에, 당첨 번호와 일치하는 개수를 cntMatch에 저장했습니다.
지워진 번호가 모두 맞으면 cntMatch + cntZero개, 모두 틀리면 확정된 cntMatch개만 맞힙니다. 이 두 값을 최고 순위, 최저 순위 순서로 등수에 바꿔 넣습니다.
여섯 개를 맞히면 1등이고 두 개를 맞히면 5등입니다. 한 개 이하를 맞힌 경우는 모두 6등으로 처리합니다.
당시 풀이 코드
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int[] answer = new int[2];
int cntZero = 0;
int cntMatch = 0;
for (int i = 0; i < 6 ; i++){
if(lottos[i] == 0) cntZero++;
for(int j = 0 ; j < 6 ; j++){
if(lottos[i] == win_nums[j]) cntMatch++;
}
}
int[] matchs = {cntMatch + cntZero , cntMatch};
int result = 0;
for (int i = 0; i < 2; i++){
if(matchs[i] == 6) result = 1;
else if (matchs[i] == 5) result = 2;
else if (matchs[i] == 4) result = 3;
else if (matchs[i] == 3) result = 4;
else if (matchs[i] == 2) result = 5;
else result = 6;
answer[i] = result;
}
return answer;
}
}