junghyun
2022.03.28
@junghyun님이 새 포스트를 작성했습니다.
오늘의 문제: 미로 찾기 or 목표 문제 풀기
class Queue { private int MAX_QUEUE_SIZE = 10; private int queue[]; private int head; private int tail; public Queue() { head = -1; tail = -1; queue = new int[MAX_QUEUE_SIZE]; } boolean is_empty() { if (head == tail) { return true; } return false; } boolean is_full() { if ((tail + 1) % MAX_QUEUE_SIZE == head) { return true; } return false; } void enqueue(int item) { if (this.is_full()) { System.out.println("큐에 더이상 데이터를 넣을 수 없습니다.\n"); return ; } tail = (tail + 1) % MAX_QUEUE_SIZE; queue[tail] = item; } int dequeue() { if (is_empty()) { System.out.println("큐가 비어있습니다. \n"); return -1; } head = (head + 1) % MAX_QUEUE_SIZE; return queue[head]; } } class Graph { int node_count = 7; int[] visited = {0, 0, 0, 0, 0, 0, 0};; int[][] graph = { {0, 1, 0, 1, 0, 0, 0}, {1, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, }; } public class Main { public static void BFS(Graph graph, int node) { Queue queue = new Queue(); graph.visited[node] = 1; System.out.printf("%d ", node); queue.enqueue(node); while (!queue.is_empty()) { int v = queue.dequeue(); for (int w = 0; w < graph.node_count; w++) { if (graph.graph[v][w] == 1 && graph.visited[w] == 0) { graph.visited[w] = 1; System.out.printf("%d ", w); queue.enqueue(w); } } } } public static void main(String[] args) { Graph graph = new Graph(); BFS(graph, 0); } }
junghyun
2022.03.25
@junghyun님이 새 포스트를 작성했습니다.
✅ 오늘의 문제: 데이터 삽입 구현하기
void insert(int nn) {     int i, j, k = 0;     if (n==MAX) {         printf("꽉 찼음\n");         return;     }     for (i = 0; i < n; i++) {         if (list[i] == nn) {             printf("같은 숫자임\n");             return;         }     }     switch(n) {     case 0:         list[0] = nn;         n++;                 return;     case 1:         if (nn < list[0]) {             list[1] = list[0];             list[0] = nn;             n++;             return;         }         else  {             list[1] = nn;             n++;                      return;         }     default :         for (i = 0; i < n; i++) {             if (list[i] < nn && nn < list[i + 1]) {                 for (j = n; j > i + 1; j--) {                     list[j] = list[j - 1];                 }                 list[i + 1] = nn;                 n++;                 return;             }             else if (num2 < list[i]) {                 for (j = n; j >= i + 1; j--) {                     list[j] = list[j - 1];                 }                 list[i] = num2;                 n++;                 return;             }         }         if (nn > list[n - 1]) {             list[n] = nn;             n++;             return;         }         break;     } } sbu07043 님의 코드를 참고하였습니다!
junghyun
2022.03.24
@junghyun님이 새 포스트를 작성했습니다.
우선순위 큐
class Heap { static int MAX_HEAP_SIZE = 101; int[] arr; int heap_count = 0; public Heap(){ this.arr = new int[MAX_HEAP_SIZE]; } private boolean compare_with_parent(int index) { if (index <= 1) return false; int parent_index = index / 2; if (this.arr[index] < this.arr[parent_index]) return true; else return false; } public boolean insert(int data) { int insert_index, parent, tmp; this.heap_count++; if (this.heap_count == 1) { this.arr[this.heap_count] = data; return true; } this.arr[this.heap_count] = data; insert_index = this.heap_count; while (this.compare_with_parent(insert_index)) { parent = insert_index / 2; tmp = this.arr[insert_index]; this.arr[insert_index] = this.arr[parent]; this.arr[parent] = tmp; insert_index = parent; } return true; } private int compare_with_child(int index) { int left, right; left = 2 * index; right = 2 * index + 1; if (left >= this.heap_count && right >= this.heap_count) { return 0; } if (right >= this.heap_count) if (this.arr[left] < this.arr[index]) return left; if (this.arr[left] < this.arr[right]) return left; else return right; } public int pop() { int index = 1, child_index, root, terminal_data, tmp; root = this.arr[1]; terminal_data = this.arr[this.heap_count]; this.arr[this.heap_count] = -1; this.arr[1] = terminal_data; this.heap_count--; while (true) { child_index = compare_with_child(index); if (child_index == 0) break; tmp = this.arr[child_index]; this.arr[child_index] = this.arr[index]; this.arr[index] = tmp; index = child_index; } return root; } } public class Main { public static void main(String[] args) { Heap heap = new Heap(); heap.insert(1); heap.insert(3); heap.insert(4); heap.insert(5); heap.insert(6); heap.insert(7); heap.insert(8); heap.insert(9); heap.insert(10); heap.insert(11); System.out.println(heap.pop()); for (int i = 1; i < heap.heap_count+1; i++) { System.out.print(heap.arr[i] + " "); } System.out.println(); System.out.println(heap.pop()); for (int i = 1; i < heap.heap_count+1; i++) { System.out.print(heap.arr[i] + " "); } } }
junghyun
2022.03.23
@junghyun님이 새 포스트를 작성했습니다.
12-4. QUIZ : 전위 순회를 트리로
class Node { int data; Node left; Node right; public Node(int data) { this.data = data; left = null; right = null; } } class BinarySearchTree { Node root; public void setRoot(int data) { this.root = new Node(data); } public int find_node_by_recursion(Node current_node, int data) { if(current_node == null) return 0; if(data < current_node.data) return find_node_by_recursion(current_node.left, data); else if(data > current_node.data) return find_node_by_recursion(current_node.right, data); else return 1; } int find(int data) { if(find_node_by_recursion(root, data) == 0) return 0; else return 1; } void insert_node(Node current_node, int data) { if(data == current_node.data) { System.out.printf("이미 %d값이 존재합니다. 중복된 값은 삽입할 수 없습니다.\n", data); return; } if (data < current_node.data) { if (current_node.left == null) { current_node.left = new Node(data); } else { insert_node(current_node.left, data); } } else if (data > current_node.data) { if (current_node.right == null) { current_node.right = new Node(data); } else { insert_node(current_node.right, data); } } } void insert(int data) { if(root == null) { setRoot(data); } else { insert_node(root, data); } } Node get_next_node(Node node) { while (node.left != null) { node = node.left; } return node; } void delete_node(Node parent, Node current_node, int data) { if (current_node == null) { System.out.printf("트리에 %d가 존재하지 않습니다. \n", data); return; } if (data < current_node.data) delete_node(current_node, current_node.left, data); else if (data > current_node.data) delete_node(current_node, current_node.right, data); else { if (current_node.left == null && current_node.right == null) { if (data < parent.data) parent.left = null; else parent.right = null; } else if (current_node.left != null && current_node.right == null) { if (data < parent.data) parent.left = current_node.left; else parent.right = current_node.left; } else if (current_node.left == null && current_node.right != null) { if (data < parent.data) parent.left = current_node.right; else parent.right = current_node.right; } else if (current_node.left != null && current_node.right != null) { Node next_node = get_next_node(current_node.right); current_node.data = next_node.data; delete_node(current_node, current_node.right, next_node.data); } } } void delete(int data) { if (root == null) System.out.printf("트리에 노드가 존재하지 않습니다."); else delete_node(null, root, data); } } // 데이터 탐색, 삽입, 제거 public class BinarySearchTree1 { static void in_order(Node node) { if (node == null) { return; } in_order(node.left); System.out.printf("%d ", node.data); in_order(node.right); return; } public static void main(String[] args) { BinarySearchTree BST = new BinarySearchTree(); BST.setRoot(7); BST.insert(3); BST.insert(1); BST.insert(5); BST.insert(10); BST.insert(8); BST.insert(4); BST.insert(12); BST.insert(15); BST.delete(7); in_order(BST.root); } }
junghyun
2022.03.22
@junghyun님이 새 포스트를 작성했습니다.
#11-3. QUIZ ✅ 오늘의 문제: 트리의 높이 구하기
class Node { int data; Node left; Node right; public Node(int data) { this.data = data; left = null; right = null; } } public class Main { public static Node initTree() { Node root = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); root.left = node2; root.right = node3; Node node4 = new Node(4); Node node5 = new Node(5); node2.left = node4; node2.right = node5; return root; } public static void pre_order(Node node) { if (node == null) { return; } System.out.printf("%d ", node.data); pre_order(node.left); pre_order(node.right); } public static void in_order(Node node) { if (node == null) { return; } in_order(node.left); System.out.printf("%d ", node.data); in_order(node.right); } public static void post_order(Node node) { if (node == null) { return; } post_order(node.left); post_order(node.right); System.out.printf("%d ", node.data); } public static void main(String[] args) { Node root = initTree(); // pre_order(root); // in_order(root); post_order(root); } }
junghyun
2022.03.21
@junghyun님이 새 포스트를 작성했습니다.
#10-2. QUIZ ✅ 오늘의 문제: 이중 링크드 리스트
class Node { public Object data; public Node pointer; public Node(Object input) { this.data = input; this.pointer = null; } } class LinkedList { private Node head = new Node(null); public Node get(int index) { Node node = this.head.pointer; while (true) { if (index == 0) return node; if (node == null) { System.out.println("Index Error"); return null; } node = node.pointer; index--; } } public void append(int data) { Node node = head; Node newNode = new Node(data); while (true) { if (node.pointer == null) break; node = node.pointer; } node.pointer = newNode; } public void insert(int data, int index) { Node node = head; Node newNode = new Node(data); while (true) { if (node == null) { System.out.println("Index error"); return; } if (index == 0) { Node nextNode = node.pointer; node.pointer = newNode; newNode.pointer = nextNode; break; } node = node.pointer; index--; } } void delete(int index) { if (index == 0) { head.pointer = head.pointer.pointer; } else { // get함수를 사용해, 인덱스 위치의 데이터를 얻어온다 Node node = get(index - 1); node.pointer = node.pointer.pointer; } } void print_linked_list() { Node node = head.pointer; while (node != null) { if (node.pointer != null) System.out.print(node.data + " -> "); else System.out.println(node.data); node = node.pointer; } } } public class Main { public static void main(String[] args) { LinkedList LL = new LinkedList(); LL.append(1); LL.append(2); LL.append(3); LL.print_linked_list(); LL.insert(10, 0); LL.print_linked_list(); LL.insert(20, 3); LL.print_linked_list(); LL.delete(1); LL.print_linked_list(); LL.delete(0); LL.print_linked_list(); } }
junghyun
2022.03.18
@junghyun님이 새 포스트를 작성했습니다.
#9-3. QUIZ ✅ 오늘의 문제: 고객 응대 시뮬레이터
class Queue { private int MAX_QUEUE_SIZE = 5; private int queue[]; private int head; private int tail; public Queue() { head = -1; tail = -1; queue = new int[MAX_QUEUE_SIZE]; } boolean is_empty() { if (head == tail) { return true; } return false; } boolean is_full() { if ((tail + 1) % MAX_QUEUE_SIZE == head) { return true; } return false; } void enqueue(int item) { if (this.is_full()) { System.out.println("큐에 더이상 데이터를 넣을 수 없습니다.\n"); return ; } tail = (tail + 1) % MAX_QUEUE_SIZE; queue[tail] = item; } int dequeue() { if (is_empty()) { System.out.println("큐가 비어있습니다. \n"); return -1; } head = (head + 1) % MAX_QUEUE_SIZE; return queue[head]; } } public class Main { public static void main(String[] args) { Queue queue = new Queue(); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); queue.enqueue(4); queue.enqueue(5); queue.enqueue(6); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); } }
junghyun
2022.03.17
@junghyun님이 새 포스트를 작성했습니다.
8-2. QUIZ: 도핑제 만들기
투포인터만 구현 완료했으며 슬라이딩 윈도우 구현 후 코드 바꿔두겠습니다. import java.util.Scanner; public class TwoPointer { public static void main(String[] args) { int N, arr[], M; // 수의 갯수, 배열, 구해야하는 수 int start=0, end=0, partial_sum=0, answer=0; // 수 입력받기 Scanner sc = new Scanner(System.in); N = sc.nextInt(); arr = new int [N]; for(int i=0; i<N; i++) { arr[i] = sc.nextInt(); } M = sc.nextInt(); while(start < N){ // >M이 먼저인 이유 : if조건을 건너뛰고 elif 가 먼저 실행되어야해서? if(partial_sum > M || end >= N) partial_sum -= arr[start++]; else if(partial_sum <= M) partial_sum += arr[end++]; if (partial_sum == M) answer++; } System.out.println(answer); sc.close(); } }