1. ๋ฌธ์ ์ค๋ช
[๋ฌธ์ ๋งํฌ](https://www.acmicpc.net/problem/2108)
์ฐ์ ํ๊ท , ์ค์๊ฐ, ์ต๋น๊ฐ, ๋ฒ์๋ฅผ ๊ตฌํ๋ฉด ๋๋ค.
2. ํธ๋ ์๋ฆฌ
์ฐ์ ํ๊ท -> ์ดํฉ / ๋จธ๋ฆฟ์
์ค์๊ฐ: ์ค์์ ์๋ ๋ ์ ๊ณ์ฐ
์ต๋น๊ฐ: TreeMap์ ์ฌ์ฉ Key = ์ ๋ ฅ ๋ฐ์ ๊ฐ, value = ํด๋น ๊ฐ์ด ๋์จ ํ์, TreeMap์ ์์๊ฐ ๋ณด์ฅ๋จ์ผ๋ก, ์ ๋ ฅ์ ํ๋ฒ ์ ๋ ฌํ ํ์ TreeMap์ ๋ฃ์ผ๋ฉด ๋๋ค.
๋ฒ์ : ์ต๋๊ฐ - ์ต์๊ฐ
3. ์ฝ๋ ๋ถ์
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.TreeMap;
/*
* 2108๋ฒ ํต๊ณํ
* */
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
TreeMap<Integer, Integer> map = new TreeMap<>();
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
int [] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
int acc = 0;
for (int i = 0; i < N; i++) {
acc +=arr[i];
if(map.get(arr[i]) == null){
map.put(arr[i], 1);
}else{
map.put(arr[i], map.get(arr[i])+1);
}
}
// ํ๊ท
sb.append(Math.round((float) acc /N)).append("\n");
// ์ค์๊ฐ
sb.append(arr[N/2]).append("\n");
// ์ต๋น๊ฐ
int mostButSecond = 0;
int mostCnt = 0;
boolean changeOnce = false;
for (int temp : map.keySet()){
if(mostCnt < map.get(temp)){
// ๋ชจ๋ ๊ฐ ๊ฐฑ์
mostButSecond = temp;
mostCnt = map.get(temp);
changeOnce = false;
}
else if(mostCnt == map.get(temp) && !changeOnce && mostButSecond < temp){
mostButSecond = temp;
changeOnce = true;
}
}
sb.append(mostButSecond).append("\n");
sb.append(arr[arr.length-1] - arr[0]);
System.out.println(sb);
}
}
0