[BaekJoon] #2587 - 대표값2 [Java][C++][Python]
[BaekJoon] #2587 - 대표값2 [Java][C++][Python]
1. 아이디어
평균은 합을 5로 나눈 값이고 중앙값은 오름차순으로 정렬했을 때 세 번째 수다. 모든 수가 10의 배수라 합도 10의 배수이고 5로 나누어떨어지므로 평균이 정수로 정확히 나오며, 정렬한 수열에서 세 번째 수는 인덱스 2에 있다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] arr = new int[5];
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
System.out.println(sum / 5);
System.out.println(arr[2]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<int> v(5);
for (int& x : v) cin >> x;
int sum = 0;
for (int x : v) sum += x;
sort(v.begin(), v.end());
cout << sum / 5 << '\n';
cout << v[2] << '\n';
}
1
2
3
4
5
6
7
import sys
input = sys.stdin.readline
arr = sorted(int(input()) for _ in range(5))
print(sum(arr) // 5)
print(arr[2])
This post is licensed under CC BY 4.0 by the author.