結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-19 22:34:50 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 2,607 ms / 5,000 ms |
| コード長 | 2,288 bytes |
| コンパイル時間 | 10,728 ms |
| コンパイル使用メモリ | 407,284 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-01 16:27:35 |
| 合計ジャッジ時間 | 27,231 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
use std::io::{self, Read};
fn read_stdin() -> Vec<String> {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).ok();
buffer.trim().split('\n').map(|s| s.to_string()).collect()
}
fn calc_primes(n: usize) -> Vec<usize> {
let mut search_list = (2..n + 1).collect::<Vec<usize>>();
let mut prime_list: Vec<usize> = Vec::new();
while (search_list[0] as f64) < (n as f64).sqrt() {
let head = search_list[0];
prime_list.push(head);
search_list = search_list.into_iter().filter(|x| *x % head > 0).collect::<Vec<usize>>();
}
prime_list.append(&mut search_list);
prime_list
}
#[derive(Debug, Eq, PartialEq)]
enum RunResult {
Win,
Lose,
}
impl RunResult {
fn s(&self) -> String {
match self {
Self::Win => "Win",
Self::Lose => "Lose",
}.to_string()
}
}
fn run(n: usize) -> RunResult {
if (2..n + 1)
.into_iter()
.fold(vec![2, 3], |mut acc, e| {
if calc_primes(n)
.clone()
.into_iter()
.take_while(|p| &e > &(*p + 1))
.filter(|p| acc.contains(&(&e - *p)))
.peekable()
.peek()
.is_none() && !acc.contains(&e) {
acc.push(e);
}
acc
})
.contains(&n) {
RunResult::Lose
} else {
RunResult::Win
}
}
fn main() {
let n = read_stdin()[0].parse::<usize>().unwrap();
let result = run(n);
println!("{}", result.s());
}
#[cfg(test)]
mod tests {
use super::{calc_primes, run, RunResult};
#[test]
fn test_calc_primes() {
assert_eq!(calc_primes(11), vec![2, 3, 5, 7, 11]);
assert_eq!(calc_primes(10), vec![2, 3, 5, 7]);
assert_eq!(calc_primes(2), vec![2]);
}
#[test]
fn test_run_lose() {
let loses = [2, 3, 11, 12];
for i in loses.iter() {
println!("test num: {}", i);
assert_eq!(run(*i), RunResult::Lose);
}
}
#[test]
fn test_run_win() {
let wins = [4, 5, 6, 7, 8, 9, 10, 13];
for i in wins.iter() {
println!("test num: {}", i);
assert_eq!(run(*i), RunResult::Win);
}
}
}