#1546: 평균

#1546: 평균

Java 15
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] array = new int[sc.nextInt()];

        for(int i = 0; i < array.length; i++){
            array[i] = sc.nextInt();
        }

        int m = Arrays.stream(array).max().getAsInt();
        double[] result = new double[array.length];
        for(int i = 0; i < array.length; i++){
            result[i] = (double) array[i]/m*100;
        }

        double sum = 0;
        for(int i = 0; i < array.length; i++){
            sum += result[i];
        }

        System.out.println(sum / array.length);
    }
}

초심을 조금식 잃어가는것 같다 (´。_。`)

Rust
use std::io::Read;

fn main() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s
        .split_whitespace()
        .map(|i| i.parse::<i32>().unwrap());

    let n = it.next().unwrap();
    let v = (0..n)
        .map(|_| it.next().unwrap())
        .collect::<Vec<_>>();
    let m = *v.iter().max().unwrap() as f64;

    print!("{:.6}", v
        .iter()
        .map(|&x| x as f64 / m * 100.0)
        .sum::<f64>() / n as f64);
}
1 Like
JavaScript
process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  var base = Number(lines[0])
  var init = lines[1].split(" ").map(x=>Number(x))
  var max = init.sort((x,y)=>y-x)[0]
  var jj = init.map(x=>x/max*100).reduce((x,y)=>x+y)
  console.log(jj/base) 
});