프로그래머스_C#/Level_1
[프로그래머스 C#] 나누어 떨어지는 숫자 배열
최애뎡
2021. 9. 1. 00:10
728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/12910
코딩테스트 연습 - 나누어 떨어지는 숫자 배열
array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요. divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하
programmers.co.kr
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] arr, int divisor) {
int[] answer = new int[] {};
List<int> temp = new List<int>();
foreach(int item in arr)
if (item % divisor == 0) temp.Add(item);
if (temp.Count == 0) temp.Add(-1);
answer = temp.OrderBy(x => x).ToArray();
return answer;
}
}
음 순수 배열로만 풀고 싶은데 좀 애매하니까..
반응형