๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 18310_์•ˆํ…Œ๋‚˜

๋ฟŒ์•ผ._. 2023. 9. 26. 11:04

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/18310)

< ์•ˆํ…Œ๋‚˜ >

 

๋ฌธ์ œ ํ’€์ด 

 

์•ˆํ…Œ๋‚˜๋ฅผ ์„ค์น˜ํ•˜์—ฌ ๊ฑฐ๋ฆฌ์˜ ์ดํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋ ค๋ฉด ์ค‘๊ฐ„ ์œ„์น˜์— ์•ˆํ…Œ๋‚˜๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค.

 

 my solution (Java)

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

public class _18310_ { // ์•ˆํ…Œ๋‚˜

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int n = Integer.parseInt(bf.readLine());

		st = new StringTokenizer(bf.readLine());
		int arr[] = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}

		Arrays.sort(arr);

		int idx = 0;
		if (n % 2 == 0)
			idx = n / 2 - 1;
		else
			idx = n / 2;

		System.out.println(arr[idx]);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์ง‘์˜ ์ˆ˜
arr : ์ง‘ ์œ„์น˜ ์ €์žฅ
idx : ์•ˆํ…Œ๋‚˜ ์„ค์น˜ํ•  ์œ„์น˜

 

- ์ง‘์˜ ์ˆ˜(n) ์ž…๋ ฅ

- ์ง‘์˜ ์œ„์น˜ ์ž…๋ ฅ ํ›„ ์ €์žฅ(arr)

- arr ์ •๋ ฌ

- ์ง‘์˜ ์ˆ˜๊ฐ€ ์ง์ˆ˜์ผ ๋•Œ 2๋กœ ๋‚˜๋ˆˆ ํ›„ 1 ๋บ€ ๊ฐ’, ํ™€์ˆ˜์ผ ๋•Œ 2๋กœ ๋‚˜๋Š” ๊ฐ’์„ idx๋กœ ์ €์žฅ

- arr [idx] ์ถœ๋ ฅ