평균구하기
x함수를 완성해서 매개변수 array의 평균값을 return하도록 만들어 보세요.
어떠한 크기의 array가 와도 평균값을 구할 수 있어야 합니다.
배열 합을 구해서 배열 크기를 나누어주면된다.
xxxxxxxxxx
public class GetMean {
public int getMean(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++)
sum += array[i];
return sum / array.length;
}
public static void main(String[] args) {
int x[] = { 5, 4, 3 };
GetMean getMean = new GetMean();
// 아래는 테스트로 출력해 보기 위한 코드입니다.
System.out.println("평균값 : " + getMean.getMean(x));
}
}
'공부 > algorithm' 카테고리의 다른 글
[programmers] 수박수박수박수박수박수? (0) | 2018.05.21 |
---|---|
[programmers]최대값과 최소값 (0) | 2018.05.21 |
[programmers] 서울에서 김서방찾기 (0) | 2018.05.21 |
[JUNGOL] 정보올림피아드&알고리즘 문제 풀이 몇가지 (0) | 2018.05.21 |
[백준 알고리즘] 4149. 큰 수 소인수분해 (2) | 2018.04.19 |
댓글