Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

정유경 / 10월 4주차 / 월 #296

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions JungYouKyoung/BOJ/boj10159.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main_10159저울 {

private static int[][] matrix;
private static int n;

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine()); // 물건의 개수
int m = Integer.parseInt(br.readLine()); // 미리 측정된 물건 쌍의 개수

matrix = new int[n + 1][n + 1];

for (int i = 0; i < m; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int m1 = Integer.parseInt(st.nextToken()); // 물건1
int m2 = Integer.parseInt(st.nextToken()); // 물건2

matrix[m1][m2] = 1; // 간선 표시
}

for (int i = 1; i < matrix.length; i++) {
int res = answer(i);
sb.append(n-res).append("\n");
}
System.out.println(sb.toString());



}// end main

private static int answer(int k) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

부모, 자식을 카운팅하는 코드가 거의 일치하는데 answer이라는 메서드 두 번 호출로 각각의 값을 반환시켜서 처리하는건 어떨까요?

int child = 0;
boolean[] isVisited = new boolean[n+1];
//자식코드
Queue<Integer> queue = new ArrayDeque<Integer>();
queue.offer(k); //시작점은 1

while(!queue.isEmpty()) {
int current = queue.poll();

for (int i = 1; i < matrix.length; i++) {
if(!isVisited[i] && matrix[current][i] == 1) { //자식코드면
queue.offer(i); //자식 들어감
isVisited[i] = true;
child++;
}
}
}

//부모
int parent = 0;
boolean[] isVisited2 = new boolean[n+1];
//자식코드
Queue<Integer> queue2 = new ArrayDeque<Integer>();
queue2.offer(k); //시작점은 1

while(!queue2.isEmpty()) {
int current = queue2.poll();

for (int i = 1; i < matrix.length; i++) {
if(!isVisited2[i] && matrix[i][current] == 1) { //자식코드면
queue2.offer(i); //자식 들어감
isVisited2[i] = true;
parent++;
}
}
}

return (child + parent + 1);

}

}// end class
55 changes: 55 additions & 0 deletions JungYouKyoung/BOJ/boj12919.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

private static String t;
private static String sb;
static boolean flag = false;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sb = br.readLine();
t = br.readLine();

solved(t);
System.out.println((flag)?1:0);

}//end main

private static void solved(String t) { //t->s
StringBuilder answer = new StringBuilder();
answer.append(t);

//기저조건
if(answer.toString().equals(sb.toString())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.equals로 스트링 비교 처리가 깔끔할 거 같은데
혹시 StringBuilder를 쓰시는 이유가 있나요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 경우에 equals를 사용해서 equals 비용이 무조건 O(n)이라고 생각해서 문제가 되지 않을까 했는데 자바7부터 길이가 다르면 false를 리턴하는 빠른 처리가 생겼었다네요..

하나 알아갑니다^^

@IntrinsicCandidate
    public static boolean equals(byte[] value, byte[] other) {
        if (value.length == other.length) {
            for (int i = 0; i < value.length; i++) {
                if (value[i] != other[i]) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

flag = true;
return;
}

if(answer.length() <= sb.length()) {
return;
}

//문자열 맨앞에 B가 있으면 B를 삭제할 수 있음 BAB -> BA, BA -> A
//맨앞에 B가 있으면 reverse() 후 맨뒤 B 삭제
if(answer.charAt(0) == 'B') {
StringBuilder sub = new StringBuilder();
sub.append(answer);
sub.reverse();
solved(sub.substring(0,sub.length()-1));
}

//문자열의 맨 뒤에 A가 있으면 A를 삭제할 수 있음
if(answer.charAt(answer.length()-1) == 'A') {
StringBuilder sub = new StringBuilder();
sub.append(answer);
solved(sub.substring(0, sub.length()-1));
}



}

}//end class
52 changes: 52 additions & 0 deletions JungYouKyoung/BOJ/boj2467.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) throws NumberFormatException, IOException {
// 산성용액 : 1 ~ 1_000_000_000 양의 정수
// 알칼리성 : -1 ~ -1_000_000_000 음의 정수

// 특성 값이 0에 가장 가까운 용액을 만들려고 한다
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine(), " ");

long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(st.nextToken());
}

int s = 0; // 인덱스 1
int e = n - 1; // 인덱스 2
long answer = Long.MAX_VALUE; // 최종 0과 가까운 값
int ai = 0; // 값이 될 것 담아두는 인덱스 1
int aj = 0; // 값이 될 것 담아두는 인덱스 2
long target = 0;

// System.out.println(Arrays.toString(arr));
while (s<e) {

target = arr[s] + arr[e]; // 두 포인 터

if (answer > Math.abs(target)) {
answer = Math.abs(target); // answer 값 바꾸어줌
ai = s;
aj = e;
}
if(target >= 0) {
e--;
}else {
s++;
}


}
System.out.println(arr[ai] + " " + arr[aj]);

}

}