Baekjoon - 18110

Tadap·2023년 9월 20일
0

Baekjoon

목록 보기
22/94
post-custom-banner

문제

Solved.ac class2++

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int sum = 0;
		int size = Integer.parseInt(br.readLine());
		if (size == 0) {
			System.out.println(0);
			return;
		}
		ArrayList<Integer> array = new ArrayList<>();
		for (int i = 0; i < size; i++) {
			array.add(Integer.parseInt(br.readLine()));
		}

		int truncation = Math.round((float)size / 100 * 15);

		array.sort(Comparator.naturalOrder());
		removeTruncation(truncation, array);

		for (Integer e : array) {
			sum += e;
		}

		System.out.println(Math.round((float)sum / array.size()));

	}

	private static void removeTruncation(int truncation, ArrayList<Integer> array) {
		for (int i = 0; i < truncation; i++) {
			array.remove(0);
			array.remove(array.size()-1);
		}
	}
}

시간초과

2차시도

시간초과. ArrayList를 배열로 변경

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int sum = 0;
		int size = Integer.parseInt(br.readLine());
		if (size == 0) {
			System.out.println(0);
			return;
		}
		int[] array = new int[size];
		for (int i = 0; i < size; i++) {
			array[i] = Integer.parseInt(br.readLine());
		}

		int truncation = Math.round((float)size / 100 * 15);

		Arrays.sort(array);

		for (int i = truncation; i < size - truncation; i++) {
			sum += array[i];
		}

		System.out.println(Math.round((float)sum / (size - (2 * truncation))));

	}

}

성공

post-custom-banner

0개의 댓글