-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8_AliceAndBobsSillyGame.java
More file actions
64 lines (54 loc) · 1.51 KB
/
day8_AliceAndBobsSillyGame.java
File metadata and controls
64 lines (54 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
/*
* Complete the sillyGame function below.
*/
static String sillyGame(int n) {
/*
* Write your code here.
*/
boolean isPrime[] = new boolean[n + 1];
Arrays.fill(isPrime , true);
int count = 0;
isPrime[0] = false;
isPrime[1] = false;
for(int i = 2; i*i <= n; i++)
{
for(int j = 2*i; j <= n; j += i)
{
isPrime[j] = false;
}
}
for(int i = 0; i <= n; i++)
{
if(isPrime[i])
{
count++;
}
}
if(count % 2 == 0)
{
return "Bob";
}
else
{
return "Alice";
}
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int g = Integer.parseInt(scanner.nextLine().trim());
for (int gItr = 0; gItr < g; gItr++) {
int n = Integer.parseInt(scanner.nextLine().trim());
String result = sillyGame(n);
bufferedWriter.write(result);
bufferedWriter.newLine();
}
bufferedWriter.close();
}
}