๐Ÿ‘ฉ‍๐Ÿ’ปProgramming/Coding Test

[C#][Programmers] ์•ฝ์ˆ˜ ๊ตฌํ•˜๊ธฐ

taesooya 2022. 12. 19.

์ •์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, n์˜ ์•ฝ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์€ ๋ฐฐ์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

๋‚ด๊ฐ€ ํ‘ผ ๋ฐฉ๋ฒ•

using System;
using System.Collections.Generic;

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

 

๋žŒ๋‹ค ์‚ฌ์šฉ ์˜ˆ์‹œ

using System;
using System.Linq;

public class Solution {
    public int[] solution(int n) {
        int[] answer = Enumerable.Range(1, n).Where(x => n % x == 0).ToArray();
        return answer;
    }
}

 

Enumerable.Range

https://learn.microsoft.com/ko-kr/dotnet/api/system.linq.enumerable.range?view=net-7.0 

 

Enumerable.Range(Int32, Int32) ๋ฉ”์„œ๋“œ (System.Linq)

์ง€์ •๋œ ๋ฒ”์œ„ ๋‚ด์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

learn.microsoft.com

IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

 

๋Œ“๊ธ€