본문 바로가기

CS/Algorithm

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

반응형

https://programmers.co.kr/learn/courses/30/lessons/77484

 

코딩테스트 연습 - 로또의 최고 순위와 최저 순위

로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호

programmers.co.kr

 

문제 설명이 길어서 어려운 문제인줄 알고 긴장했다가 긴장 확 풀렸음..

class Solution {
        static int one = 6;
        static int two = 5;
        static int three = 4;
        static int four = 3;
        static int five = 2;
    
    public int[] solution(int[] lottos, int[] win_nums) 
    {
        int[] answer = new int[2];
        int count = 0;
        int zero = 0;
        for(int i = 0; i < lottos.length; i++)
        {
            if(lottos[i] == 0)
            {
                zero++;
                continue;
            }
            for(int j = 0; j < win_nums.length; j++)
            {
                if(lottos[i] == win_nums[j])
                {
                    count++;
                }
            }
        }
        
        int high = zero+count;
        int low = count;
        
        answer[0] = getNumber(high);
        answer[1] = getNumber(low);
        
        return answer;
    }
    
    public static int getNumber(int count)
    {
        if(count == one)
        {
            return 1;
        } 
        else if(count == two)
        {
            return 2;
        }
        else if(count == three)
        {
            return 3;
        }
        else if(count == four)
        {
            return 4;
        }
        else if(count == five)
        {
            return 5;
        }
        else {
            return 6;
        }
    }
}
반응형

'CS > Algorithm' 카테고리의 다른 글

다트 게임  (0) 2021.05.11
카카오 문자열 압축 문제  (0) 2021.05.07
모의고사  (0) 2021.05.04
키패드 누르기  (0) 2021.05.04
신규 아이디 추천  (0) 2021.05.03