Problem solving/Algorithms

[프로그래머스] 약수 구하기 (C#)

Young_A 2025. 7. 17. 14:02

목차

    프로그래머스 - 약수구하기

    Problem Description

    정수 n이 매개변수로 주어질 때, n의 약수를 오름차순으로 담은 배열을 return하도록 solution 함수를 완성해주세요.

     

    Constraints:

    1 ≤ n ≤ 10,000

     

     

    My Solution (C#)

    using System;
    using System.Collections.Generic;
    
    public class Solution {
        public int[] solution(int n) {
            List<int> factors = new List<int>();
    
            for(int i= 1; i <= n; i++)
            {
                if(n%i == 0)
                {
                    factors.Add(i);
                }
            }
    
            int[] answer = factors.ToArray();
    
            return answer;
        }
    }

     

    Generic 네임스페이스 사용 깜빡했었음