TIL 2023/11/15 알고리즘

YEONGDO·2023년 11월 15일

1. 짝수의 합 (프로그래머스)

코드

class Solution {
    public int solution(int n) {
        int answer = 0;
        for (int i=0; i <= n; i++) {
            if (i % 2 == 0) {
                answer += i;
            }
        }
        return answer;
    }
}

2. 배열의 평균값 (프로그래머스)

코드

class Solution {
    public double solution(int[] numbers) {
        double answer = 0;
        int tot=0;
        for (int i=0; i<numbers.length; i++) {
            tot+=numbers[i];
        }
        answer=(double) tot/numbers.length;
        return answer;
    }
}

3. 짝수와 홀수 (프로그래머스)

코드

class Solution {
    public String solution(int num) {
        String answer = "";
        if (num % 2 == 0) {
            answer = "Even";
        } else {
            answer = "Odd";
        }
        return answer;
    }
}

4. 평균 구하기 (프로그래머스)

코드

class Solution {
    public double solution(int[] arr) {
        double answer = 0;
        int sum = 0;
        
        for (int i=0; i<arr.length; i++) {
            sum += arr[i];
        }
        
        answer = (double)sum / arr.length;
        return answer;
    }
}

느낀점

프로그래머스 코테를 0레벨부터 풀어봤는데 0레벨이라 쉬울 줄 알았는데.. 모르는 것도 은근 많아서 좀.. 그랬다.. 그래도 모르는 문제 있어도 풀이 보고 완전히 이해하면 내 것! 차근차근 알고리즘 연습을 계속해서 해나가야겠다.

profile
개발 블로그

0개의 댓글