結果
| 問題 |
No.275 中央値を求めよ
|
| ユーザー |
|
| 提出日時 | 2020-10-22 11:18:21 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 1,326 bytes |
| コンパイル時間 | 14,943 ms |
| コンパイル使用メモリ | 378,668 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-21 09:15:53 |
| 合計ジャッジ時間 | 15,152 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 38 |
ソースコード
use std::io::{self, BufReader, BufWriter, Read, Write};
fn main() {
let stdin = io::stdin();
let mut stdin = BufReader::new(stdin.lock());
let stdout = io::stdout();
let mut stdout = BufWriter::new(stdout.lock());
let n = read_usize(&mut stdin);
let mut a: Vec<i32> = (0..n).map(|_| read_i32(&mut stdin)).collect();
a.sort();
if n % 2 == 0 {
let i = n / 2;
let avg = (a[i - 1] + a[i]) as f64 / 2.0;
writeln!(&mut stdout, "{}", avg).unwrap();
} else {
writeln!(&mut stdout, "{}", a[n / 2]).unwrap();
}
}
fn read_usize<R: Read>(reader: &mut R) -> usize {
reader
.bytes()
.filter_map(|b| b.ok())
.skip_while(|b| b.is_ascii_whitespace())
.take_while(|b| !b.is_ascii_whitespace())
.map(|b| b as usize - 48)
.fold(0, |acc, x| acc * 10 + x)
}
fn read_i32<R: Read>(reader: &mut R) -> i32 {
let mut iter = reader
.bytes()
.filter_map(|b| b.ok())
.skip_while(|b| b.is_ascii_whitespace())
.take_while(|b| !b.is_ascii_whitespace());
let buf = iter.next().unwrap();
if buf == b'-' {
iter.map(|b| b as i32 - 48).fold(0, |acc, x| acc * 10 - x)
} else {
iter.map(|b| b as i32 - 48)
.fold(buf as i32 - 48, |acc, x| acc * 10 + x)
}
}