[백준] 2587번 - 대표값2 [Java][C++]
[백준] 2587번 - 대표값2 [Java][C++]
1. 문제 풀이
평균과 중앙값을 구하는 문제로 평균은 다섯 수의 합을 $5$ 로 나누면 되고 중앙값은 다섯 수를 정렬한 후 세 번째 값을 찾으면 된다.
2. 코드
1. 구현 [Java]
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]);
}
}
2. 구현 [C++]
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(false);
cin.tie(nullptr);
vector<int> v(5);
int sum = 0;
for (int i = 0; i < 5; i++) {
cin >> v[i];
sum += v[i];
}
sort(v.begin(), v.end());
cout << sum / 5 << '\n';
cout << v[2] << '\n';
}
3. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 100 ms | 14220 KB | 476 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2020 KB | 325 B |
This post is licensed under CC BY 4.0 by the author.