-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from Kut-PS-Study/Dltmd202
Feat: Add solution of #118
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Stack; | ||
|
||
public class Main { | ||
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
static StringBuilder sb = new StringBuilder(); | ||
|
||
public static void main(String[] args) throws IOException { | ||
int n = Integer.parseInt(br.readLine()); | ||
|
||
int[] data = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
data[i] = Integer.parseInt(br.readLine()); | ||
} | ||
|
||
make(n, data); | ||
} | ||
|
||
private static void make(int n, int[] data) { | ||
Stack<Integer> stack = new Stack<>(); | ||
int cursor = 1; | ||
|
||
try { | ||
for (int i = 0; i < n; i++) { | ||
if (stack.isEmpty() || stack.peek() < data[i]) { | ||
for (int j = cursor; j <= data[i]; j++) { | ||
stack.push(j); | ||
sb.append("+\n"); | ||
} | ||
cursor = data[i] + 1; | ||
} | ||
while (!stack.isEmpty() && stack.peek() >= data[i]) { | ||
if(stack.pop() > data[i]) throw new RuntimeException(); | ||
sb.append("-\n"); | ||
} | ||
} | ||
System.out.println(sb.toString().trim()); | ||
}catch (Exception e){ | ||
System.out.println("NO"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.*; | ||
|
||
public class Main { | ||
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
static StringBuilder sb = new StringBuilder(); | ||
|
||
public static void main(String[] args) throws IOException { | ||
Set<Integer> set = new HashSet<>(); | ||
int n = Integer.parseInt(br.readLine()); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
for (int i = 0; i < n; i++) { | ||
set.add(Integer.parseInt(st.nextToken())); | ||
} | ||
|
||
int m = Integer.parseInt(br.readLine()); | ||
st = new StringTokenizer(br.readLine()); | ||
for (int i = 0; i < m; i++) { | ||
if(set.contains(Integer.parseInt(st.nextToken()))){ | ||
sb.append("1\n"); | ||
} else { | ||
sb.append("0\n"); | ||
} | ||
} | ||
System.out.println(sb.toString().trim()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.StringTokenizer; | ||
|
||
public class Main { | ||
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
static StringBuilder sb = new StringBuilder(); | ||
|
||
public static void main(String[] args) throws IOException { | ||
int n = Integer.parseInt(br.readLine()); | ||
int maxValue = Integer.MIN_VALUE; | ||
int[] data = new int[n]; | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
for (int i = 0; i < n; i++) { | ||
data[i] = Integer.parseInt(st.nextToken()); | ||
maxValue = Math.max(data[i], maxValue); | ||
} | ||
|
||
boolean[] table = primeTable(maxValue); | ||
int cnt = 0; | ||
|
||
for (int i = 0; i < n; i++) { | ||
cnt += table[data[i]] ? 1: 0; | ||
} | ||
System.out.println(cnt); | ||
} | ||
|
||
public static boolean[] primeTable(int maxValue){ | ||
boolean[] table = new boolean[maxValue + 1]; | ||
int pivot = (int) Math.sqrt(maxValue) + 1; | ||
Arrays.fill(table, 2, table.length, true); | ||
|
||
for (int i = 2; i < pivot; i++) { | ||
for (int j = 2; i * j <= maxValue; j++) { | ||
table[i * j] = false; | ||
} | ||
} | ||
return table; | ||
} | ||
} |