본문 바로가기

공부하자/Codility

[Codility] Lesson11. CountNonDivisible (C#)

문제: app.codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_non_divisible/

 

CountNonDivisible coding task - Learn to Code - Codility

Calculate the number of elements of an array that are not divisors of each element.

app.codility.com

 

결과: 66%.. 다시 풀자..

 

            Dictionary<int, int> listA = new Dictionary<int, int>();
            for (int i = 0; i < A.Length; i++)
            {
                if (!listA.ContainsKey(A[i]))
                {
                    int count = 0;
                    for (int j = 0; j < A.Length; j++)
                    {
                        if (i != j && A[i] % A[j] != 0)
                            count++;
                    }
                    listA.Add(A[i], count);
                }
            }

            int[] result = new int[A.Length];
            for (int i = 0; i < A.Length; i++)
            {
                listA.TryGetValue(A[i], out result[i]);
            }

            return result;