BOJ 1546 평균

1 개요[ | ]

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

2 C++[ | ]

#include <iostream>
using namespace std;
int main() {
    int N;
    int a[1000];
    scanf("%d", &N);
    for(int i=0; i<N; i++) scanf("%d", &a[i]);
    int max = 0;
    int sum = 0;
    for(int i=0; i<N; i++) {
        if( a[i]>max ) max=a[i];
        sum += a[i];
    }
    printf("%f", 100.0*sum/N/max);
}

3 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 );
    }
}

4 Perl[ | ]

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

5 PHP[ | ]

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

6 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 }}