Skip to content

Commit

Permalink
Feat: Add solution of #118
Browse files Browse the repository at this point in the history
  • Loading branch information
Dltmd202 committed Jul 25, 2022
1 parent 9342776 commit 20c3c5e
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/BaekJoon/Dltmd202/10773/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.Stack;

/**
* 제로
* https://www.acmicpc.net/problem/10773
*/
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 k = Integer.parseInt(br.readLine());
Stack<Integer> stack = new Stack<>();

for (int i = 0; i < k; i++) {
int data = Integer.parseInt(br.readLine());

if(data == 0) stack.pop();
else stack.push(data);
}

System.out.println(
stack.stream()
.mapToInt(Integer::intValue).sum()
);
}
}
68 changes: 68 additions & 0 deletions src/BaekJoon/Dltmd202/10814/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
* 나이순 정렬
* https://www.acmicpc.net/problem/10814
*/
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());
Person[] people = new Person[n];

for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
people[i] = new Person(
Integer.parseInt(st.nextToken()),
st.nextToken()
);
}

Arrays.stream(people)
.sorted(
Comparator.comparingInt(Person::getAge)
.thenComparing(
Comparator.comparingInt(Person::getId))
)
.forEach(sb::append);

System.out.println(sb.toString().trim());
}

static class Person{
static int seq = 0;
int id;
int age;
String name;

public Person(int age, String name) {
this.id = seq++;
this.age = age;
this.name = name;
}

public int getAge(){
return age;
}

public String getName(){
return name;
}

public int getId(){
return id;
}

@Override
public String toString() {
return age + " " + name + "\n";
}
}
}
46 changes: 46 additions & 0 deletions src/BaekJoon/Dltmd202/10816/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
* 숫자 카드 2
* https://www.acmicpc.net/problem/10816
*/
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());

Map<Integer, Integer> map = new HashMap<>();
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
int data = Integer.parseInt(st.nextToken());
if(!map.containsKey(data)){
map.put(data, 1);
} else {
map.put(data, map.get(data) + 1);
}
}

int m = Integer.parseInt(br.readLine());

st = new StringTokenizer(br.readLine());

for (int i = 0; i < m; i++) {
int data = Integer.parseInt(st.nextToken());
if(!map.containsKey(data))
sb.append(0)
.append(" ");
else {
sb.append(map.get(data))
.append(" ");
}
}
System.out.println(sb.toString());
}
}
37 changes: 37 additions & 0 deletions src/BaekJoon/Dltmd202/10828/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

/**
* 스택
* https://www.acmicpc.net/problem/10828
*/
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());
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
String command = br.readLine();
if (command.startsWith("push")){
int data = Integer.parseInt(command.substring(5));
stack.push(data);
} else {
if (command.equals("top")) {
sb.append(!stack.isEmpty() ? stack.peek() : -1);
} else if (command.equals("pop")){
sb.append(!stack.isEmpty() ? stack.pop() : -1);
} else if(command.equals("size")){
sb.append(stack.size());
} else if(command.equals("empty")){
sb.append(stack.isEmpty() ? 1: 0);
}
sb.append("\n");
}
}
System.out.println(sb.toString());
}
}

0 comments on commit 20c3c5e

Please sign in to comment.