[programmers]로또의 최고 순위와 최저 순위(C#)

김근면·2022년 7월 14일
0

Programmers

목록 보기
2/5
post-thumbnail

🔗Link

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

💻Code

✍1번 풀이

public class Solution
    {
        
        public static int[] solution(int[] lottos, int[] win_nums)
        {
            int sameValue = 0;
            int zeroValue = 0;
            int[] rank = new int[7] { 6, 6, 5, 4, 3, 2, 1 };

            for (int i = 0; i < lottos.Length; i++)
            {
                if (lottos[i] == 0)
                {
                    zeroValue++;
                    continue;
                }

                for (int j = 0; j < win_nums.Length; j++)
                {
                    if (lottos[i] == win_nums[j])
                    {
                        sameValue++;
                        break;
                    }
                }
            }

            int min = sameValue;
            int max = min + zeroValue;

            int[] answer = new int[2] { rank[max], rank[min] };

            return answer;

✍ lottos에서 0이 들어가는 만큼 zeroValue에 값을 할당해주고 win_nums에는 lottos와 같은 값이 있는지 대조하여 sameValue에 할당해준다.
✍ lottos와 win_nums에 같은 값이 들어가 있다면 min에 sameValue를 할당해준다.
✍ max에는 min + zeroValue를 할당해준다.

✍ 위와 같이 선언했다면 min = 2(최저순위), max = 4(최고순위)이다.
✍ 이제 rank 배열의 Index값을 출력하면된다.

✍2번 풀이

using System;

public class Solution {
    public int[] solution(int[] lottos, int[] win_nums) 
    {
        int[] answer;
        
        int a = 7;
        int b = 7;
        
        for (int i = 0; i < lottos.Length; i++)
        {
            if (lottos[i] == 0) a--;
            else if (Array.Exists(win_nums, x => x == lottos[i]))
            {
                a--; b--;
            }
        }
        
        if (a == 7) a = 6;
        if (b == 7) b = 6;
        
        answer = new int[2] {a, b};
        
        return answer;
    }
}

✍3번 풀이

using System;
using System.Linq;
public class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int right = 0;
        int zeroCount =0;
        for(int i=0; i<lottos.Length; i++)
        {
            if(lottos[i]==0) 
            {
                zeroCount++;
            }
            if(lottos.Contains(win_nums[i]))
            {
                right++;
            }
        }
        int MaxCollect = zeroCount+right;
        int MinCollect = right;
        if(MinCollect==0) MinCollect++;
        if(MaxCollect==0) MaxCollect++;
        int[] answer = new int[] {7-MaxCollect,7-MinCollect};
        return answer;
    }
}

📌Solution

1번 풀이 참고자료
2번 풀이 참고자료
3번 풀이 참고자료

profile
cheer about the man right next to you

0개의 댓글