프로그래머스 숫자카드 나누기. C#

안또니오·2022년 11월 12일
0

CSharp

목록 보기
2/3

문제

공약수 배열을 얻어낸 뒤,
상대방 배열에서 나눠지면 continue;
아닌 경우 그 값을 return하도록 하였다.

35번 케이스만 풀리지 않는다.

11월 12일. 오후 1시 5분 수정.
35번 케이스는 중복된 값이 있는 케이스로 추정된다.
중복값이 있는 경우 제대로 처리가 안 되어 틀린 답이 리턴돼 틀렸던 것으로 보인다.

public class Solution
    {
        public int solution(int[] arrayA, int[] arrayB)
        {
            int[] arrA = DivideArray(arrayA);
            int[] arrB = DivideArray(arrayB);
            int A = GetNotDivede(arrayA, arrB);
            int B = GetNotDivede(arrayB, arrA);

            if (A == B) return 0;
            if (A > B) return A;
            else return B;
        }
        public int[] DivideArray(int[] arr)
        {
            Array.Sort(arr);
            List<int> list = new List<int>();
            for (int i = arr[0]; i >= 2; i--)
            {
                for (int j = 0; j < arr.Length; j++)
                {
                    if (arr[j] % i != 0)
                    {
                        break;
                    }
                    else
                    {
                        if (j == arr.Length - 1)
                        {
                            list.Add(i);
                        }
                    }
                }
            }
            return list.ToArray();
        }
        public int GetNotDivede(int[] upper, int[] lower)
        {
            if (upper.Length < 1) return 0;
            if (lower.Length < 1) return 0;
            for (int i = 0; i < upper.Length; i++)
            {
                for (int j = 0; j < lower.Length; j++)
                {
                //continue를 break로 수정.
					//if (upper[i] % lower[j] == 0) continue;
                    if (upper[i] % lower[j] == 0) break;
                    else
                    {
                        return lower[j];
                    }
                }
            }

            return 0;
        }
    }
profile
2020. 11월 공부시작.

0개의 댓글