結果

問題 No.275 中央値を求めよ
ユーザー cm-kojimat
提出日時 2020-08-13 15:25:53
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 1,499 bytes
コンパイル時間 14,005 ms
コンパイル使用メモリ 378,328 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-09 19:15:44
合計ジャッジ時間 14,730 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;

struct Input {
    n: usize,
    a: Vec<i64>,
}

fn read_input(cin_lock: &mut StdinLock) -> Input {
    let n = next_token(cin_lock);
    Input {
        n,
        a: next_vector_token(cin_lock, n),
    }
}

fn solve(mut input: Input) {
    input.a.sort();
    let ans = match input.n % 2 {
        0 => {
            let a0 = input.a[input.n / 2 - 1];
            let a1 = input.a[input.n / 2];
            ((a0 + a1) as f32) / 2.0
        }
        _ => input.a[input.n / 2] as f32,
    };
    println!("{}", ans);
}

fn next_token<T: FromStr>(cin_lock: &mut StdinLock) -> T {
    cin_lock
        .by_ref()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>()
        .parse::<T>()
        .ok()
        .unwrap()
}

fn next_vector_token<T: FromStr>(cin_lock: &mut StdinLock, n: usize) -> Vec<T> {
    let cin = cin_lock.by_ref();

    (0..n)
        .map(|_| {
            cin.bytes()
                .map(|c| c.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect::<String>()
                .parse::<T>()
                .ok()
                .unwrap()
        })
        .collect()
}

fn main() {
    let cin = stdin();
    let mut cin_lock = cin.lock();
    let input = read_input(&mut cin_lock);
    solve(input);
}
0