Algorithm 110

[C언어] - 원형 덱 double-ended queue

🔵 원형 덱 double-ended queue 원형 큐는 항상 하나의 자리를 비워둠 front, rear에서 모두 삽입, 삭제가 가능한 큐 front, rear 모두 0으로 초기화 공백 상태 > front == rear 포화 상태 > front = (rear + 1) % M큐크기 🟡 원형 덱 코드 add_rear 뒤로 이동 > 빈 공간에 삽입 get_front 뒤로 이동 > 해당 값 삭제 get_rear 삭제 > 앞으로 이동 add_front 삽입 > 앞으로 이동 #include #include #define MAX_SIZE 10 typedef int element; typedef struct { int front, rear; element data[MAX_SIZE]; } QueueType; void i..

Algorithm 2023.01.12

[C언어] - 원형 큐 Circular Queue

🔵 원형 큐는 항상 하나의 자리를 비워둠 > 포화 상태 - front가 rear보다 하나 앞에 있는 경우 front == (rear + 1) % M큐크기 > 공백 상태 - front == rear 인 경우 🟡 원형 큐 코드 #include #include #define MAX_SIZE 10 typedef int element; typedef struct { int front, rear; element data[MAX_SIZE]; } QueueType; void init(QueueType* q) { q->front = -1; q->rear = -1; } int is_full(QueueType* q) { return q->front == (q->rear + 1) % MAX_SIZE;// 원형 큐의 경우 } ..

Algorithm 2023.01.11

[C언어] Stack 스택 - 후위표기식 계산 postfix / 문자열 숫자 정수형으로 바꾸기

🔵후위표기식 계산 조건 피연산자만 스택에 push 연산자인 경우 피연산자 2개 pop > 계산 > 계산 결과 push 스택에 남아있는 피연산자 pop > 총 결과 🟡후위표기식 계산 코드 #include #include #include #define MAX_SIZE 100 typedef int element; typedef struct { int top; element data[MAX_SIZE]; } StackType; void init(StackType* s) { s->top = -1; } int is_full(StackType *s) { return s->top == MAX_SIZE - 1; } int is_empty(StackType* s) { return s->top == -1; } void pus..

Algorithm 2023.01.11

[C언어] Stack 스택 - 중위표기식 infix에서 후위표기식 postfix으로 변환

🔵 infix -> postfix 변환 조건 스택에 연산자만 push 괄호인 경우 왼쪽 괄호는 무조건 스택에 push 오른쪽 괄호인 경우 왼쪽괄호 삭제될 때까지 위에 쌓인 연산자 pop 괄호가 아닌 경우 (+, -, *, /) 연산자 우선순위 비교하기 스택에 들어있는 연산자 우선순위가 더 큰 경우 해당 연산자 pop 🟡 중위표기식 후위표기식으로 변환하는 코드 #include #include #include #define MAX_SIZE 100 typedef char element; typedef struct { int top; element data[MAX_SIZE]; } StackType; void init(StackType* s) { s->top = -1; } int is_full(StackType*..

Algorithm 2022.12.23

[C언어] Stack 스택 - 괄호 검사

🔵 괄호 검사 조건 오른쪽 괄호 만날 때까지 왼쪽 괄호 push 오른쪽 괄호 만났을 경우 스택에 들어있는 왼쪽 괄호 pop 💥오류인 경우 스택이 비어있을 때 괄호 종류가 맞지 않을 때 오른쪽 괄호 끝까지 스캔했을 경우 💥오류인 경우 스택이 비어있지 않았을 때 🟡 괄호 검사 코드 #include #include #define MAX_SIZE 100 typedef char element; typedef struct { int top; element data[MAX_SIZE]; } StackType; void init(StackType* s) { s->top = -1; } int is_full(StackType* s) { return s->top == MAX_SIZE - 1; } int is_empty(Sta..

Algorithm 2022.12.13

[C언어] Stack 스택 - 동적 배열 스택 / 동적 메모리 할당 malloc, realloc, free

🟡 동적 배열 스택 코드 시간 복잡도 O(1) #include #include typedef int element; typedef struct { element* data; int top; int capacity; // 현재 크기 } StackType; void init(StackType* s) { s->top = -1; s->capacity = 1; s->data = (element*)malloc(s->capacity * sizeof(element)); } int is_full(StackType *s) { if (s->top == s->capacity - 1) return 1; else return 0; } int is_empty(StackType *s) { if (s->top == -1) return..

Algorithm 2022.12.13

#백준 1764, 파이썬 / set 집합 자료형 교집합, 합집합, 차집합

n, m = map(int, input().split()) # 딕셔너리에 듣도 못한 사람의 이름, 수 입력받기 names = {} for i in range(n): name = input() names[name] = 1 ans = [] for j in range(m): name = input() if names.get(name) == None: # 딕셔너리에 있는 이름일 경우 듣보잡 continue else: ans.append(name) # 결과 출력 print(len(ans)) for answer in ans: print(answer) 왜 틀렸다고 나오는지 모르겠다ㅠ 혹시나 이유를 아시는 분은 댓글 달아주시면 좋을 것 같습니다ㅠ 다른 분들의 풀이를 검색해봤더니 set 집합 자료형을 통해 교집합을 구하..

Algorithm/Baekjoon 2022.10.20

#백준 10816, 파이썬 / 딕셔너리 이용

# 백준 집합과 맵 : 10816번 - 숫자 카드 2 # 딕셔너리 이용 # 가지고 있는 숫자 카드 개수 n n = int(input()) # n개의 숫자 카드에 적혀있는 정수 cards 리스트에 넣기 cards = list(map(int, input().split(' '))) # 가지고 있는 숫자 카드와 비교할 개수 m m = int(input()) # 가지고 있는 숫자 카드인지 구해야 할 m개의 정수 guess = list(map(int, input().split(' '))) # 몇 개 가지고 있는 비교하기 ans = {} for card in cards: if card in ans: ans[card] += 1 else: ans[card] = 1 # 결과 출력하기 for i in guess: if an..

Algorithm/Baekjoon 2022.10.12
728x90