[프로그래머스/java]평균 구하기
리트리버J
·2020. 12. 20. 14:48
728x90
정수를 담고 있는 배열 arr의 평균값을 return하는 함수, solution을 완성해보세요.
1
2
3
4
5
6
7
8
9
10
11
|
class Solution {
public double solution(int[] arr) {
double answer = 0;
// for-each로 int배열 하나씩 더하기
for(int temp : arr) {
answer += temp;
}
// return 시 배열 길이만큼 나누기
return answer / arr.length;
}
}
|
cs |
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
728x90
'알고리즘 > 코딩테스트' 카테고리의 다른 글
[프로그래머스/java]최대공약수와 최소공배수 ※유클리드 호제법 (0) | 2020.12.20 |
---|---|
[프로그래머스/java]콜라츠 추측 (0) | 2020.12.20 |
[프로그래머스/java]하샤드 수 (0) | 2020.12.20 |
[프로그래머스/java]핸드폰 번호 가리기 (0) | 2020.12.20 |
[프로그래머스/java]행렬의 덧셈 (0) | 2020.12.20 |