본문 바로가기
Algorithm/Programmers

[프로그래머스/JAVA] LV1 - 로또의 최고 순위와 최저 순위 / JAVA

by 김비누! 2022. 4. 1.

프로그래머스 LV1 - 로또의 최고 순위와 최저 순위

문제

코드

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int[] answer = {0, 0};
        int unknownCnt = 0;
        int correctCnt = 0;

        for(int i = 0; i < 6; i++){
            if(lottos[i] == 0){
                unknownCnt++;
                continue;
            } else {
                int currentNum = lottos[i];
                for(int j = 0; j < 6; j++){
                    if(currentNum == win_nums[j]){
                        correctCnt++;
                        break;
                    }

                }
            }
        }

        answer[0] = 7-(correctCnt+unknownCnt);
        answer[1] = 7-(correctCnt);

        /* 숫자 1개만 맞을 경우(계산결과 6)와 하나도 맞지 않을 경우(계산결과 7) 둘다 6등(낙첨) */
        if(answer[0] == 7) answer[0] = 6;
        if(answer[1] == 7) answer[1] = 6;

        return answer;
    }
}

깔끔하고 가독성좋게 푸신 분들이 많았다.
나이브한 코드 반성😓
맵, 딕셔너리를 잘 못쓰는데 연습해야겠다.

댓글