[프로그래머스 C#] Lv.0 짝수는 싫어요

김병찬·2022년 11월 3일
0

프로그래머스 Lv.0

목록 보기
17/100

🎯문제설명

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


❌제한사항

  • 1 ≤ n ≤ 100

💬입출력 예

nresult
10[1, 3, 5, 7, 9]
15[1, 3, 5, 7, 9, 11, 13, 15]

💬입출력 예 설명

입출력 #1

  • 10 이하의 홀수가 담긴 배열 [1, 3, 5, 7, 9]를 return합니다.

입출력 #1

  • 15 이하의 홀수가 담긴 배열 [1, 3, 5, 7, 9, 11, 13, 15]를 return합니다.

🔥나의 풀이

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(i % 2 == 1)
            {
                list.Add(i);
            }
        }
        
        int[] answer = new int[list.Count];
        for(int i = 0; i < list.Count; i++)
        {
            answer[i] = list[i];
        }
        return answer;
    }
}

출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

profile
[중요한건 꺾이지 않는 마음] Unity Developer

0개의 댓글