문제 풀기/C#

58. 소수 만들기

kagan-draca 2025. 2. 7. 14:57

 

기본 틀 :

using System;

class Solution
{
    public int solution(int[] nums)
    {
        int answer = -1;

        // [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
        System.Console.WriteLine("Hello C#");

        return answer;
    }
}

 

문제를 보고 3개의 수의 합이 중복이 없어야 하는줄 알고

 

HashSet으로 문제를 풀었다가

 

23점이 나와서 깜짝 놀랐다...

 

(문제를 잘 보면 nums에 중복된 숫자가 들어있지 않다는건데...)

 

using System;
using static System.Math;
using System.Collections.Generic;

class Solution
{
    public int solution(int[] nums)
    {
        List<int> list = new List<int>();
        
        for(int i = 0; i < nums.Length - 2; i++)
        {
            for(int j = i + 1; j < nums.Length - 1; j++)
            {
                for(int k = j + 1; k < nums.Length; k++)
                {
                    list.Add(nums[i] + nums[j] + nums[k]);   
                }
            }
        }
        
        int count = 0;
        
        for(int i = 0; i < list.Count; i++)
        {
            bool check = true;
            for(int j = 2; j <= (int)Math.Sqrt(list[i]); j++)
            {
                if(list[i] % j == 0)
                {
                    check = false;
                    break;
                }
            }
            if(check) count++;
        }
        
        return count;
    }
}

'문제 풀기 > C#' 카테고리의 다른 글

60. 기사단원의 무기  (0) 2025.02.10
59. 덧칠하기  (0) 2025.02.07
57. 모의고사  (0) 2025.02.07
56. 과일 장수  (0) 2025.02.06
55. 카드 뭉치  (0) 2025.02.06