[BaekJoon] 2587번 - 대표값2 [Java][C++]
[BaekJoon] 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(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';
}
3. 디버깅
없음.
4. 참고
없음.
This post is licensed under CC BY 4.0 by the author.