BOJ 4344 평균은 넘겠지

1 개요[ | ]

BOJ 4344 평균은 넘겠지
  • 평균이 넘는 학생들의 퍼센테이지를 출력하는 문제

2 C++[ | ]

#include <iostream>
using namespace std;
int main() {
    int C;
    scanf("%d", &C);
    int N;
    int a[1000];
    for(int tc=1; tc<=C; tc++) {
        scanf("%d", &N);
        for(int i=0; i<N; i++) scanf("%d", &a[i]);
        int sum = 0;
        for(int i=0; i<N; i++) sum += a[i];
        float avg = 1.0*sum/N;
        int count = 0;
        for(int i=0; i<N; i++) {
            if( a[i]>avg ) count++;
        }
        printf("%.3f%\n", 100.0*count/N);
    }
}

3 Java[ | ]

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for( int i=0; i<n; i++ ) {
            int m = sc.nextInt();
            int[] nums = new int[m];
            for( int j=0; j<m; j++ ) {
                nums[j] = sc.nextInt();
            }
            double avg = Arrays.stream(nums).average().getAsDouble();
            int cnt = 0;
            for( int j=0; j<m; j++ ) {
                if( nums[j]>avg ) cnt++;
            }
            System.out.format("%.3f%%\n", 100.0*cnt/m);
        }
    }
}

4 Perl[ | ]

use List::Util qw( sum );
foreach $i (1 .. <>) {
        @scores = (split / /, <>);
        $n = shift @scores;
        $avg = sum(@scores) / $n;
        foreach (@scores) { our $cnt; $cnt++ if ($_ > $avg); }
        printf( "%.3f%%\n", (100*$cnt/$n) );
}

5 PHP[ | ]

<?php
fscanf(STDIN,'%d',$t);
for($i=0; $i<$t; $i++) {
    $scores = explode(' ',rtrim(fgets(STDIN)));
    $n = array_shift($scores);
    $avg = array_sum($scores) / $n;
    $cnt = count(array_filter($scores, function($s) use ($avg) {
        return $s > $avg;
    }));
    echo number_format( 100*$cnt/$n, 3 ) . "%\n";
}

6 Python[ | ]

t = int(input())
for i in range(t):
    scores = list(map(int,input().split()))
    n = scores[0]
    scores = scores[1:]
    avg = sum(scores)/n
    cnt = len([s for s in scores if s>avg])
    print( "%.3f" % (100*cnt/n), end='%\n' )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}