์ ์ 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);
'๐ฉโ๐ปProgramming > Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#][Programmers] ์ธ๋ฑ์ค ๋ฐ๊พธ๊ธฐ (0) | 2022.12.25 |
---|---|
[C#][Programmers] ๋๋ฌธ์์ ์๋ฌธ์ (0) | 2022.12.12 |
[C#][Programmers] ๋ชจ์ ์ ๊ฑฐ (0) | 2022.11.28 |
[C#][Programmers] ๋ฐฐ์ด์ ์ ์ฌ๋ (0) | 2022.11.26 |
[C#][Programmers] ์ ๊ณฑ์ ํ๋ณํ๊ธฐ (0) | 2022.11.26 |
๋๊ธ