<문제>
정수 배열 numbers가 주어집니다. numbers에서 서로 다른 인덱스에 있는 두 개의 수를 뽑아 더해서 만들 수 있는 모든 수를 배열에 오름차순으로 담아 return 하도록 solution 함수를 완성해주세요.
<정답>
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] numbers) {
List answers = new List { };
for (int i=0;i<numbers.Length-1;i++)
{
for(int j= i+1;j<numbers.Length;j++)
{
answers.Add(numbers[i] + numbers[j]);
}
}
nswers = answers.Distinct().ToList(); //중복을 제거
answers.Sort(); //오름차순으로 정렬
return answers.ToArray(); //List를 배열로 정렬
}
'코딩연습 > 프로그래머스 코딩테스트' 카테고리의 다른 글
[프로그래머스] Level1 2016년 (0) | 2021.02.21 |
---|---|
[프로그래머스] Level1 K번째 수 (0) | 2021.02.20 |
[프로그래머스]Level1 시저암호 (0) | 2021.02.16 |
[프로그래머스]Level1 이상한 문자 만들기 (0) | 2021.02.09 |
[프로그래머스]Level1 모의고사 (0) | 2021.02.04 |