티스토리 뷰
- 에라토스테네스의 체
public ArrayList<Integer> eratos(int n) {
ArrayList<Integer> result = new ArrayList<>();
ArrayList<Boolean> check = new ArrayList<>();
for (int i = 0; i <= n; i++) {
check.add(true);
}
for (int i = 2; i <= n; i++) {
if (!check.get(i)) continue;
result.add(i);
for (int j = i * 2; j <= n; j += i) {
check.set(j, false);
}
}
return result;
}
- GCD 구하기(유클리드 호제법)
public int getGCD(int a, int b) {
while (b > 0) {
int t = a % b;
a = b;
b = t;
}
return a;
}
- LCM 구하기
public int getLCM(int a, int b) {
return a / getGCD(a, b) * b;
}
- Permutation
public void permutation(int[] arr, int depth, int n, int r, boolean[] visited, int[] out) {
if (depth == r) {
System.out.println(Arrays.toString(out));
return;
}
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
visited[i] = true;
out[depth] = arr[i];
permutation(arr, depth + 1, n, r, visited, out);
visited[i] = false;
}
}
- Combination
void combination(int[] arr, boolean[] visited, int depth, int n, int r) {
if (r == 0) {
for (int i = 0; i < n; i++) {
if (visited[i]) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
return;
}
if (depth == n) return;
visited[depth] = true;
combination(arr, visited, depth + 1, n, r - 1);
visited[depth] = false;
combination(arr, visited, depth + 1, n, r);
}
- sqrt(바빌로니아 법)
static double sqrt(int n) {
double result = 1;
for (int i = 0; i < 10; i++) {
result = (result + (n / result)) / 2;
}
return result;
}
- BFS
import java.io.*;
import java.util.*;
public class Main {
public static class Pair {
int x;
int cnt;
Pair(int x, int cnt) {
this.x = x;
this.cnt = cnt;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int max = (int) 2e6 + 1;
int[] visited = new int[max];
Arrays.fill(visited, -1);
Queue<Pair> q = new LinkedList<>();
visited[n] = 0;
q.add(new Pair(n, 0));
while (!q.isEmpty()) {
Pair cur = q.remove();
if (cur.x == k) break;
int[] nexts = new int[]{2 * cur.x, cur.x + 1, cur.x - 1};
for (int i = 0; i < nexts.length; i++) {
int next = nexts[i];
if (next >= 0 && next < max && visited[next] == -1) {
visited[next] = cur.cnt + 1;
q.add(new Pair(next, cur.cnt + 1));
}
}
}
bw.write(visited[k] + "\n");
bw.flush();
bw.close();
}
}
- Trie
class Node {
HashMap<Character, Node> child;
boolean isTerminal;
public Node() {
this.child = new HashMap<>();
this.isTerminal = false;
}
}
class Trie {
Node root;
public Trie() {
this.root = new Node();
}
public void insert(String str) {
Node cur = this.root;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
cur.child.putIfAbsent(c, new Node());
cur = cur.child.get(c);
if (i == str.length() - 1) {
cur.isTerminal = true;
}
}
return;
}
public boolean search(String str) {
Node cur = this.root;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (cur.child.containsKey(c)) cur = cur.child.get(c);
else return false;
if (i == str.length() - 1 && !cur.isTerminal) {
return false;
}
}
return true;
}
public void delete(String str) {
boolean ret = this.delete(this.root, str, 0);
if (ret) System.out.println(str + " 삭제 완료");
else System.out.println(str + " 단어가 없습니다");
}
public boolean delete(Node node, String str, int idx) {
char c = str.charAt(idx);
if (!node.child.containsKey(c)) return false;
Node cur = node.child.get(c);
idx++;
if (idx == str.length()) {
if (!cur.isTerminal) return false;
cur.isTerminal = false;
if (cur.child.isEmpty()) {
node.child.remove(c);
}
} else {
if (!this.delete(cur, str, idx)) {
return false;
}
if (!cur.isTerminal && cur.child.isEmpty()) {
node.child.remove(c);
}
}
return true;
}
}
- 플로이드 워셜
'기타' 카테고리의 다른 글
제로베이스 백엔드 스쿨 4기 5개월차 후기 (0) | 2022.12.04 |
---|---|
Big Sur 업데이트 후 SQL Developer 실행 오류 뜰 때 (0) | 2021.03.25 |
modular 활용 (0) | 2021.02.25 |
펜윅, pii 관련 자료 (0) | 2021.02.25 |
실력 떡상! (0) | 2021.02.22 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- boj 14868
- boj 10473
- 백준 12713
- 백준 14868
- 사탕상자
- 백준 2243
- 백준 1106
- boj 3006
- 백준 9345
- 터보소트
- 백준 1280
- boj 9345
- 부트 캠프
- boj 2336
- 백준 2336
- 백준 10775
- 백준 3006
- boj 16562
- 인간 대포
- 디지털 비디오 디스크
- 백준 10473
- boj 10775
- boj 1106
- Ugly Numbers
- boj 2243
- boj 12713
- 백준 16562
- 제로베이스 스쿨
- 제로베이스 백엔드 스쿨
- boj 1280
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함