Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

프로그래머스 짝수는 싫어요(자바) 본문

JavaAlgorithm

프로그래머스 짝수는 싫어요(자바)

MyDiaryYo 2023. 10. 30. 15:47

 

 

class Solution {
    public int[] solution(int n) {
        int[] answer = new int[(n+1)/2]; //배열 크기 설정
        int a = 0;     //배열 인덱스를 위한 변수 생성
        
        for(int i=0; i<=n; i++){
            if(i%2 != 0){        //홀수일 경우
                answer[a] = i;   // ex) answer[0]=1; answer[1]=3;
                a++;
            }
        }
        return answer;
    }
}