Skip to content

Commit

Permalink
Merge pull request #127 from Kut-PS-Study/Dltmd202
Browse files Browse the repository at this point in the history
Feat: Add solution of #118
  • Loading branch information
Dltmd202 authored Jul 9, 2022
2 parents 3179c91 + 7e50518 commit b55b470
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/BaekJoon/Dltmd202/1874/Main.java
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");
}
}
}
30 changes: 30 additions & 0 deletions src/BaekJoon/Dltmd202/1920/Main.java
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());
}
}
43 changes: 43 additions & 0 deletions src/BaekJoon/Dltmd202/1978/Main.java
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;
}
}

0 comments on commit b55b470

Please sign in to comment.