로또의 최고 순위와 최저 순위 (자바)

김재현·2023년 11월 29일
0

알고리즘 풀이

목록 보기
36/89
post-thumbnail

문제

업로드중..

정답 코드

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
      int cnt=0;

        // 0을 제외하고 당첨된 번호의 개수
        for(int i=0;i<lottos.length;i++) {
            for (int j=0;j< win_nums.length;j++) {
                if (lottos[i]==win_nums[j]) {
                        cnt++;
                }
            }
        }

        int lottosZeroCnt=0;

        // 0의 개수
        for(int i=0;i< lottos.length;i++) {
            if (lottos[i]==0) {
                lottosZeroCnt++;
            }
        }

        // 당첨 개수 카운트
        int[] yesCnt = new int[2];
        yesCnt[0]=cnt+lottosZeroCnt;
        yesCnt[1]=cnt;

        int[] answer = new int[2];
        answer[0]=7-yesCnt[0];
        answer[1]=7-yesCnt[1];

        if(answer[0]>6) answer[0]=6;
        if(answer[1]>6) answer[1]=6;

        return answer;
    }
}

어려운 문제는 아니었다.
마지막에 당첨 개수 카운트 할 때 5등 미만을 처리하는것만 조금 고민했던 문제!

다른 사람 코드

        int cnt1 = 0;
        int cnt2 = 0;
        for(int i : lottos) {
            if(i == 0) {
                cnt1++;
                continue;
            }
            for(int j : win_nums) {
                if(i == j) cnt2++;
            }
        }

이렇게 향상된 for문 안에 향상된 for문이 또 들어가는 구조는 처음 보았다.
다음에 나도 적용시켜 봐야지!

profile
I live in Seoul, Korea, Handsome

0개의 댓글