結果

問題 No.7 プライムナンバーゲーム
ユーザー YamaKasaYamaKasa
提出日時 2018-06-09 21:26:11
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,149 bytes
コンパイル時間 3,058 ms
コンパイル使用メモリ 79,344 KB
実行使用メモリ 56,512 KB
最終ジャッジ日時 2023-09-13 02:23:20
合計ジャッジ時間 6,410 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,416 KB
testcase_01 WA -
testcase_02 AC 163 ms
55,732 KB
testcase_03 AC 146 ms
55,952 KB
testcase_04 AC 130 ms
55,672 KB
testcase_05 WA -
testcase_06 AC 151 ms
56,024 KB
testcase_07 AC 147 ms
56,092 KB
testcase_08 AC 145 ms
56,512 KB
testcase_09 WA -
testcase_10 AC 123 ms
55,804 KB
testcase_11 AC 147 ms
55,524 KB
testcase_12 AC 160 ms
55,816 KB
testcase_13 AC 155 ms
55,976 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 158 ms
55,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		scan.close();
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 2; i <= N; i++) {
			if(isPrime(i)) {
				list.add(i);
			}
		}
		if(N == 2 || N == 3) {
			System.out.println("Lose");
			System.exit(0);
		}
		// a[i] = 1なら負け
		int []a = new int[N + 1];
		Arrays.fill(a, 0);
		a[0] = 1;
		a[1] = 1;
		a[2] = 1;
		a[3] = 1;
		for(int i = 2; i <= N; i++) {
			for(int j = 0; j < list.size(); j++) {
				int t = i - list.get(j);
				if(t <= 1) {
					a[i] = 1;
					break;
				}
				if(a[t] == 1) {
					a[i] = 0;
					break;
				}
			}
		}
		if(N == 1) {
			System.out.println("Lose");
		}else {
			System.out.println("Win");
		}

	}


	public static boolean isPrime(int n) {
	    if (n < 2) return false;
	    else if (n == 2) return true;
	    else if (n % 2 == 0) return false;

	    for (int i = 3; i < n; i += 2) {
	        if (n % i == 0) {
	            return false;
	        }
	    }

	    return true;
	}
}
0