結果

問題 No.7 プライムナンバーゲーム
ユーザー SagTokiSagToki
提出日時 2018-05-24 15:03:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 166 ms / 5,000 ms
コード長 2,441 bytes
コンパイル時間 3,261 ms
コンパイル使用メモリ 78,680 KB
実行使用メモリ 54,452 KB
最終ジャッジ日時 2024-04-09 04:28:51
合計ジャッジ時間 6,501 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
54,452 KB
testcase_01 AC 115 ms
53,648 KB
testcase_02 AC 166 ms
54,212 KB
testcase_03 AC 144 ms
54,080 KB
testcase_04 AC 134 ms
54,412 KB
testcase_05 AC 133 ms
54,160 KB
testcase_06 AC 126 ms
53,220 KB
testcase_07 AC 136 ms
54,000 KB
testcase_08 AC 138 ms
54,156 KB
testcase_09 AC 149 ms
54,188 KB
testcase_10 AC 125 ms
53,880 KB
testcase_11 AC 134 ms
54,056 KB
testcase_12 AC 156 ms
54,228 KB
testcase_13 AC 141 ms
53,192 KB
testcase_14 AC 149 ms
53,084 KB
testcase_15 AC 148 ms
54,284 KB
testcase_16 AC 165 ms
54,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;
import java.util.InputMismatchException;

public class PrimeNumber {
    
    //Nを入力して範囲が正しいか、数字で書かれているかのチェック
    public static int InputN(){
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        try{
        if(N < 2 || N > 10000){
            System.out.println("Nは2以上10000以下の数字で入力してください");
            System.exit(0);
        }
        }catch(InputMismatchException e){
            System.out.println("数字を入力してください");
            System.exit(0);
        }catch(Exception E){
            System.out.println("想定外のエラーです");
            System.exit(0);
        }
        return N;
    }
    
    //リストを生成して2~Nの素数を格納する
    public static boolean[] MakePrimeNumbers(int N){
        //長さN+1のリストを生成して初期値をすべてtrueにする
        boolean[] Answer = new boolean[N + 1];
        Arrays.fill(Answer , true);
        Answer[1] = false;
        //偶数は素数になり得ないので除外
        for (int i = 4; i <= N; i += 2) {
            Answer[i] = false;
        }
        //素数は奇数であることが前提なので奇数に範囲を絞る
        for (int i = 3; i * i <= N ; i += 2) {
            for (int j = 3 ; i * j <= N ; j += 2) {
                Answer[i * j] = false;
            }
        }
        return Answer;
    }
    
    //勝敗をつけるための処理を行う
    public static boolean Game(int N , boolean[] isPrime , boolean[] Result){
        for (int i = 2 ; i < N ; i++) {
            if (isPrime[i] == false) {
                continue;
            }
            if (!Result[N - i] && N - i != 1){
                return true;
            }
        }
        return false;
    }
    
    //最後にmainメソッドで結果を出力する
    public static void main(String[] args){
        int N = InputN();
        boolean[] isPrime = MakePrimeNumbers(N);
        boolean[] Result = new boolean[N + 1];
        Result[2] = false;
        for (int i = 3 ; i <= N ; i++) {
            Result[i] = Game(i , isPrime , Result);
        }
        //trueかfalseで勝敗を決定
        if(Result[N]){
            System.out.println("Win");
        }else{
            System.out.println("Lose");
        }
    }
}
0