로또의 최고 순위와 최저 순위

magicdrill·2024년 12월 3일
0

로또의 최고 순위와 최저 순위

알고리즘 자체는 쉬운데, 최고 등수를 저장할 때 예외를 생각하지 못했다.
처음 저장한 방식은 내가 1개를 맞추면 6등, 0개를 맞추면 7등으로 저장되게 잘못 작성했다.

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        //int[] answer = {};
        int[] answer = new int [2];
        //lottos : 내 로또 번호
        //win_nums : 당첨 로또 번호
        //최고 등수, 최저 등수 구하기
        
        //0을 제외한 맞춘 개수, 0의 개수 구하기?
        int zeroCount = 0;
        int correctCount = 0;
        int i, j;
        
        for(i = 0; i < lottos.length; i++){
            if(lottos[i] == 0){
                zeroCount++;
            }
            else{
                for(j = 0; j < win_nums.length; j++){
                    if(lottos[i] == win_nums[j]){
                        correctCount++;
                        break;
                    }
                }
            }
        }
        //System.out.println("zeroCount : " + zeroCount);
        //System.out.println("correctCount : " + correctCount);
        
        //answer[0] = 7 - (correctCount + zeroCount); //내가 다 틀리거나 하나만 맞춰서 6등일 수도 있음
        answer[0] = ((correctCount + zeroCount) > 1) ? 7 - (correctCount + zeroCount) : 6;
        answer[1] = (correctCount > 1) ? (7 - correctCount) : 6;
        
        return answer;
    }
}

0개의 댓글