BOJ 1546 평균

Ykhwong (토론 | 기여)님의 2018년 7월 28일 (토) 21:28 판

1 개요

BOJ 1546 평균

[[분류:BOJ {{{단계}}}단계]]

  • 최대값을 찾아, 그 값으로 다른 값들을 바꾼 후 평균을 구하는 문제

2 Java

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int score, sum = 0, max = 0;
        for(int i=0; i<n; i++) {
            score = sc.nextInt();
            sum += score;
            if( score>max ) max = score; 
        }
        System.out.println( 100.0 * sum / n / max );
    }
}
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int scores[] = new int[n];
        for(int i=0; i<n; i++) {
            scores[i] = sc.nextInt();
        }
        int max = 0;
        for(int i=0; i<n; i++) {
            if( scores[i]>max ) max = scores[i]; 
        }
        float sum = 0;
        for(int i=0; i<n; i++) {
            sum += 100.0 * scores[i] / max;
        }
        System.out.println( sum / n );
    }
}

3 Perl

use List::Util qw( max sum );
$n = int(<STDIN>);
@lst = (split / /, <STDIN>);
($max, $sum) = (max(@lst), sum(@lst));
printf("%.2f\n", $sum / $n * 100 / $max);

4 PHP

<?php
fscanf(STDIN,'%d',$n);
$scores = explode(' ',rtrim(fgets(STDIN)));
$max = max($scores);
$sum = array_sum($scores);
echo $sum/$n*100/$max;

5 Python

import sys
N = int(sys.stdin.readline())
lst = list(map(int,sys.stdin.readline().split()))
M = max(lst)
S = sum(lst)
print( S / N * 100 / M )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}