結果

問題 No.275 中央値を求めよ
ユーザー mottodora
提出日時 2016-10-16 20:38:47
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 987 bytes
コンパイル時間 26,555 ms
コンパイル使用メモリ 392,764 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-25 00:06:55
合計ジャッジ時間 14,419 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

fn getline() -> String{
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}

fn getvec(string: String) -> Vec<i32> {
    string.trim().split_whitespace()
        .map(|s| s.parse().unwrap())
        .collect()
}

fn bubblesort(v: &Vec<i32>) -> Vec<i32> {
    let mut dst = v.to_owned();
    for i in 0..dst.len() {
        for j in i..dst.len() {
            if dst[i] > dst[j] {
                dst.swap(i, j)
            }
        }
    }
    return dst
}

fn median_odd(v: &Vec<i32>) -> i32 {
    v[v.len()/2]
}

fn median_even(v: &Vec<i32>) -> f32 {
    let m = v[v.len()/2-1] as f32;
    let n = v[v.len()/2] as f32;
    (n+m)/2.0
}



fn main() {
    let _ = getline();
    let nums = getline();
    let nums = getvec(nums);
    let sorted = bubblesort(&nums);
    match sorted {
        ref v if v.len() % 2 == 0 => println!("{}", median_even(&sorted)),
        _                         => println!("{}", median_odd(&sorted))
    }
}
0