結果

問題 No.7 プライムナンバーゲーム
ユーザー mtwtkmanmtwtkman
提出日時 2019-10-19 22:34:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 2,979 ms / 5,000 ms
コード長 2,288 bytes
コンパイル時間 992 ms
コンパイル使用メモリ 176,376 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:45:45
合計ジャッジ時間 19,642 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2,979 ms
6,948 KB
testcase_03 AC 88 ms
6,944 KB
testcase_04 AC 17 ms
6,944 KB
testcase_05 AC 16 ms
6,948 KB
testcase_06 AC 604 ms
6,948 KB
testcase_07 AC 359 ms
6,948 KB
testcase_08 AC 128 ms
6,944 KB
testcase_09 AC 1,046 ms
6,948 KB
testcase_10 AC 1 ms
6,948 KB
testcase_11 AC 364 ms
6,944 KB
testcase_12 AC 1,931 ms
6,948 KB
testcase_13 AC 2,088 ms
6,944 KB
testcase_14 AC 2,967 ms
6,948 KB
testcase_15 AC 2,784 ms
6,948 KB
testcase_16 AC 2,481 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
        }
    }
}
0