[D4] 평균의 평균 - 18799
[D4] 평균의 평균 - 18799
문제 링크
성능 요약
메모리: 13,536 KB, 시간: 9 ms
코드길이: 813 Bytes
출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
using namespace std;
int arr[9];
int main(int argc, char** argv)
{
int test_case;
int T;
cin >> T;
for (test_case = 1; test_case <= T; ++test_case)
{
double result = 0.0;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int subsetCount = 1 << n; // 부분집합 개수
for (int mask = 1; mask < subsetCount; ++mask) { // 공집합 제외
double subsetSum = 0.0;
int subsetSize = 0;
for (int i = 0; i < n; ++i) {
if (mask & (1 << i)) {
subsetSum += arr[i];
subsetSize++;
}
}
double subsetAverage = subsetSum / subsetSize;
result += subsetAverage;
}
result /= subsetCount - 1; // 공집합 제외
cout << fixed;
cout.precision(18);
cout << "#" << test_case << " " << result << "\n";
}
return 0;
}
This post is licensed under CC BY 4.0 by the author.