BOJ 2108 통계학

1 개요[편집]

BOJ 단계별로 풀어보기
순번 문제 풀이
9단계 정렬해보기 e
1 BOJ 2750 수 정렬하기

2 BOJ 2751 수 정렬하기 2

3 BOJ 10989 수 정렬하기 3

4 BOJ 2108 통계학

5 BOJ 1427 소트인사이드

6 BOJ 1181 단어 정렬

2 Java[편집]

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int size = Integer.parseInt(br.readLine());
		int arr[] = new int[size];
		int counts[] = new int[8001];
		int i, num, sum=0, min=4000, max=-4000, max_count=0;
		for(i=0; i<size; i++) {
			num = Integer.parseInt(br.readLine());
			arr[i] = num;
			sum += num;
			if( num > max ) max=num;
			if( num < min ) min=num;
			counts[4000+num]++;
			if( counts[4000+num]>max_count ) max_count = counts[4000+num];
		}
		int freq2 = 0, freq_count = 0;
		for(i=min; i<=max && freq_count<2; i++) {
			if( counts[4000+i] != max_count ) continue;
		    freq2 = i;
		    freq_count++;
		}
		Arrays.sort(arr);
		System.out.println( Math.round(1.0D*sum/size) );
		System.out.println( arr[size/2] );
		System.out.println( freq2 );
		System.out.println( max-min );   
	}
}

3 Perl[편집]

use List::Util qw(sum max); use POSIX;
$N = int(<>);
push @a, $str=<> for (1..$N);
$c{$_}++ for @a = sort {$a <=> $b} @a;
$m = max values %c;
($c{$_} eq $m)?push @modes, $_:'' for (keys %c);
@modes = sort {$a <=> $b} @modes;
@result = (
    floor((sum(@a)*2+$N)/$N/2),
    $a[int($N/2)],
    $modes[1 % @modes],
    $a[-1] - $a[0]
);
printf("%d\n" x 4, @result);

4 PHP[편집]

<?php
$N = intval(fgets(STDIN));
$a = [];
for($i=0; $i<$N; $i++) $a[] = intval(fgets(STDIN));
sort($a);
$mean = floor((array_sum($a)*2+$N)/$N/2);
$median = $a[intdiv($N,2)];
$c = array_count_values($a);
$m = max($c);
$modes = array_filter($c, function($ele) use($m) {
    return ($ele == $m); 
});
$modes = array_keys($modes);
$mode = $modes[ 1 % count($modes) ];
$range = $a[$N-1] - $a[0];
echo $mean . "\n";
echo $median . "\n";
echo $mode . "\n";
echo $range . "\n";

5 Python[편집]

import sys
import collections
N = int(input())
a = sorted(map(int,sys.stdin))
mean = (sum(a)*2+N)//N//2
median = a[N//2]
c = collections.Counter(a)
m = max(c.values())
modes = sorted(i for i in c if c[i]==m)
mode = modes[1%len(modes)]
range = a[-1] - a[0]
print( mean )
print( median )
print( mode )
print( range )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}