๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 6177_Statistics

๋ฟŒ์•ผ._. 2024. 10. 30. 17:31
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/6177)

< Statistics >

 

๋ฌธ์ œ ํ’€์ด 

 

1. ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•˜์—ฌ N์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค.

2. ์ •๋ ฌํ•œ ํ›„ ๊ฐ€์šด๋ฐ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. (์ง์ˆ˜๊ฐœ๋ผ๋ฉด ์ค‘๊ฐ„ 2๊ฐœ์˜ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.)

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class _6177_ { // Statistics
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(bf.readLine());
		int arr[] = new int[N];
		int sum = 0;
		for (int i = 0; i < N; i++) {
			arr[i] = Integer.parseInt(bf.readLine());
			sum += arr[i];
		}
		System.out.println((double) sum / N);
		Arrays.sort(arr);
		if (N % 2 == 0) {
			sum = arr[N / 2 - 1] + arr[N / 2];
			System.out.println((double) sum / 2);
		} else {
			System.out.println(arr[N / 2]);
		}
	}
}
๋ณ€์ˆ˜)
N : ์ด ๊ฐœ์ˆ˜
arr : ๊ฐ’ ์ €์žฅ ๋ฐฐ์—ด
sum : ํ•ฉ

 

์ด ๊ฐœ์ˆ˜ N์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. N๋งŒํผ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•˜๋Š” ๋™์‹œ์— ํ•ฉ์„ ๊ตฌํ•œ๋‹ค. ํ•ฉ์„ N์œผ๋กœ ๋‚˜๋ˆˆ ํ‰๊ท ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. arr์„ ์ •๋ ฌ ํ›„ N์ด ํ™€์ˆ˜๋ผ๋ฉด ์ค‘๊ฐ„ ๊ฐ’์„, ์ง์ˆ˜๋ผ๋ฉด ์ค‘๊ฐ„ ๊ฐ’ 2๊ฐœ์˜ ํ‰๊ท ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 11235_Polling  (0) 2024.11.04
[Baekjoon] 11609_Class Time  (0) 2024.11.01
[Baekjoon] 6160_Election Time  (0) 2024.10.29
[Baekjoon] 10527_Judging Troubles  (0) 2024.10.28
[Baekjoon] 11968_High Card Wins  (0) 2024.10.25