提出詳細
ソースコード
import java.io.*;
class TeraCoderF {
BufferedReader in;
boolean[] primes = new boolean[1001]; // falseが素数
boolean[] superPrimes = new boolean[1001]; // trueがsuper素数
TeraCoderF() {
in = new BufferedReader(new InputStreamReader(System.in));
for (int i = 2; i <= 1000; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
primes[i] = true;
break;
}
}
}
int countPrime = 1; // 2を勘定に入れる
for (int i = 3; i <= 1000; i++) {
if (primes[i] == true) continue;
if (primes[i] == false) countPrime++;
if (primes[countPrime] == false) {
superPrimes[i] = true;
}
}
}
void run() {
int[] values;
values = readIntTokens();
int numCases = values[0];
for (int i = 1; i <= numCases; i++) {
values = readIntTokens();
System.out.println("Case #" + i + ":");
for (int j = 3; j <= values[0]; j++) {
if (superPrimes[j]) System.out.println(j);
}
}
close();
}
public static void main(String[] args) {
TeraCoderF app = new TeraCoderF();
app.run();
}
int[] readIntTokens() {
String str;
int[] intTokens;
try {
str = in.readLine();
if (str == null || str.equals("")) return null;
} catch (IOException ioe) {
// ioe.printStackTrace();
return null;
}
String[] tokens = str.split(" ");
intTokens = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
intTokens[i] = Integer.parseInt(tokens[i]);
}
return intTokens;
}
void close() {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
提出情報
提出出力結果
テストケース情報