#include <string>
#include <vector>
#include <numeric>
using namespace std;
double solution(vector<int> numbers) {
double answer = accumulate(numbers.begin(), numbers.end(), 0);
return answer / numbers.size();
}
numeric 라이브러리를 추가하면 accumulate() 함수를 사용할 수 있다.
accumulate() 함수로 배열의 합을 구하고 vector.size() 를 사용해 배열의 크기로 나누면 평균이 나온다.
'코딩테스트' 카테고리의 다른 글
[프로그래머스][Python] 약수의 개수와 덧셈 (약수의 개수가 짝수인지 홀수인지 판별하기) (0) | 2022.10.07 |
---|---|
[프로그래머스][C++] 두 수의 나눗셈 (int형 정수 나누기 소수점 얻기) (0) | 2022.10.02 |
[프로그래머스][Python] 핸드폰 번호 가리기 (0) | 2022.10.01 |
[프로그래머스][Python] 정수 내림차순으로 배치하기 (0) | 2022.09.25 |
[프로그래머스][Python] 문자열 내 p와 y의 개수 (0) | 2022.09.25 |