주녘공부일지

[프로그래머스 C#] Lv.1 로또의 최고 순위와 최저 순위 본문

Programmers - C#/CodingTest Lv.1

[프로그래머스 C#] Lv.1 로또의 최고 순위와 최저 순위

주녘 2023. 10. 31. 21:11
728x90

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1. 정답코드 및 핵심 아이디어, 유의사항

직관적인 문제, 주석 참조

    using System;
    using System.Collections.Generic;

    public class Solution
    {
        public int[] solution(int[] lottos, int[] win_nums)
        {
            List<int> myList = new List<int>(lottos); // 내가 가진 로또 번호 리스트
            List<int> lottoList = new List<int>(win_nums); // 로또 당첨 번호 리스트

            int zeroCount = 0; // 알아볼 수 없는 번호의 개수
            int count = 0; // 로또 당첨 개수
            
            // 0의 개수 카운트
            while (myList.Contains(0))
            {
                myList.Remove(0);
                zeroCount++;
            }
            
            // 로또 당첨 개수 카운트
            foreach (int num in myList)
                if (lottoList.Contains(num))
                    count++;
            
            int min = Function(count);
            int max = Function(count + zeroCount);

            return new int[2] { max, min };
        }

        // 맞는 개수에 따른 순위를 반환하는 메서드 (조건)
        public int Function(int num)
        {
            if (num < 2)
                return 6;

            return 7 - num;
        }
    }

 

728x90