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

hmin27 #44

Open
wants to merge 11 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
4 changes: 2 additions & 2 deletions WEEK1-B/hmin27/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#week01
# week01

#boj11279
# boj11279

1. Priority Queue 우선순위 큐
= queue with priority for pop
Expand Down
171 changes: 171 additions & 0 deletions WEEK3/hmin27/boj11723.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ADD 1
#define REMOVE 2
#define CHECK 4
#define TOGGLE 8
#define ALL 16
#define EMPTY 32

typedef struct _Elem {
int data;
struct _Elem *next;
} Elem;

Elem *head;


int _check(int val) { //���� linked list�� �ִ��� Ȯ�� �� ������ 1, ������ 0 return
Elem *ptr;

if (head != NULL) {
ptr = head;
while (ptr != NULL) {
if (val == ptr->data) return 1;
ptr = ptr->next;
}

}

return 0;
}

void _add(int val) {
Elem *ptr;
ptr = head;

Elem *newNode = (Elem *)malloc(sizeof(Elem));
newNode->data = val;
newNode->next = NULL;


if (head == NULL) head = newNode;
else {
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = newNode;
}

}

void _remove(int val) {
Elem *curPtr, *prePtr;

curPtr = head->next;
prePtr = head;

while (curPtr != NULL) {
if (val == curPtr->data) {
prePtr->next = curPtr->next;
free(curPtr);
break;
}
prePtr = curPtr;
curPtr = curPtr->next;

}

// free(prePtr);
}

int compare(char *opr) {

if (!strcmp(opr, "add")) return ADD;
else if (!strcmp(opr, "remove")) return REMOVE;
else if (!strcmp(opr, "check")) return CHECK;
else if (!strcmp(opr, "toggle")) return TOGGLE;
else if (!strcmp(opr, "all")) return ALL;
else if (!strcmp(opr, "empty")) return EMPTY;
}

void operation(int setOpr, int value) {

if ( (setOpr & CHECK) || (setOpr & ADD) || (setOpr & REMOVE) || (setOpr & TOGGLE) ) {

if (_check(value)) {
if (setOpr & CHECK) printf("1\n");
else if(setOpr & REMOVE | setOpr & TOGGLE) {
_remove(value); //���� �̹� �ִٸ� �����ϱ�
}
}
else {
if (setOpr & CHECK) printf("0\n");
else if(setOpr & ADD | setOpr & TOGGLE) {
_add(value); //���� ���ٸ� �߰��ϱ�
}
}
}

else if (setOpr & ALL) {
Elem *tail, *ptr;
int i, lastIndex = 0;

if (head != NULL) {
ptr = head->next;
for (i = 0; i<20; i++) {
ptr->data = i + 1;
if (ptr->next == NULL) {
lastIndex = i+1;
break;
}

ptr = ptr->next;
}

}

tail = head;
for (i = lastIndex; i < 20; i++) {
Elem * temp = (Elem *)malloc(sizeof(Elem));
temp->data = i+1;
temp->next = NULL;


tail->next = temp;
tail = temp;
}

tail->next = NULL;
}

else if (setOpr & EMPTY) {
Elem *curDel, *nextDel;

if (head != NULL) {
curDel = head;
nextDel = head->next;

free(curDel);

while (nextDel != NULL) {
curDel = nextDel;
nextDel = nextDel->next;
free(curDel);
}

}

head = NULL;
}
}


int main() {
char opr[10];
int repetition, value, setOpr;

scanf("%d", &repetition);

for (int i = 0; i < repetition; i++) {
scanf("%s %d", opr, &value);
setOpr = compare(opr);

operation(setOpr, value);
}

return 0;
}
68 changes: 68 additions & 0 deletions WEEK3/hmin27/boj15953.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
int rankA(int *pz1, int a) {
int i, rankA = 1;
int resA;

if (a > 21 || a == 0) resA = 0;
else {
for (i = 1; i < 7; i++) {
if (a <= rankA) {
resA = pz1[i - 1];
break;
}
rankA += i + 1;
}
}

return resA;
}

int rankB( int *pz2, int b) {
int i, rankB = 1;
int resB;
int temp = 1;

if (b > 31 || b == 0) resB = 0;
else {
for (i = 1; i < 6; i++) {
if (b <= rankB) {
resB = pz2[i - 1];
break;
}

temp *= 2;
rankB += temp;
}
}

return resB;
}

int main()
{
int *queue;
int prize1[6] = { 500, 300, 200, 50, 30, 10},
prize2[5] = { 512, 256, 128, 64, 32};
int front, rear;
int size, i, resA, resB;

scanf("%d", &size);

queue = (int *)malloc(sizeof(int) * size * 2);
front = 0;
rear = -1;

for (i = 0; i < size * 2; i++) {
scanf("%d", &queue[i]);
rear++;
}

while(front < rear) {
resA = rankA(prize1, queue[front++]);
resB = rankB(prize2, queue[front++]);
printf("%d\n", (resA+resB)*10000);
}

return 0;
}
70 changes: 70 additions & 0 deletions WEEK3/hmin27/boj2941.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <string.h>

int main()
{
char *croAlp[8] = { "c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z=" };
char stack[101]; //stack ó���� ���� $ �߰�
char topElem, nextElem;
char str[10];
int top = 1;
int temp, cnt = 0, flag = 0;

stack[0] = '$';

while (1) {

temp = getchar();
if (temp == '\n') {
--top;
break;
}
else {
stack[top] = temp;
top++;
}
}

while (1) {
topElem = stack[top--];
//stack�� ����� ���
if (topElem == '$') break;

//ũ�ξ�Ƽ�� ���ĺ��� ���
else if (topElem == '=' || topElem == '-' || (topElem == 'j' && (stack[top] == 'n' || stack[top] == 'l'))) {

nextElem = stack[top--];
sprintf_s(str, sizeof(str), "%c%c", nextElem, topElem);

for (int i = 0; i < 8; i++) {
if (!strcmp(croAlp[i], str)) {
if (i == 7) {
if (stack[top] == 'd') top--; //dz=�� z= ����
}
++cnt;
++flag;
break;
}
}

if (!flag) {
sprintf_s(str, sizeof(str), "%s%c", str, topElem);

for (int i = 0; i < 8; i++) {
if (!strcmp(croAlp[i], str)) {
++cnt;
break;
}
}
}

flag == 0;

}
//ũ�ξ�Ƽ�� ���ĺ��� �ƴ� ���ĺ��� ���
else cnt++;
}


printf("%d", cnt);
}
36 changes: 36 additions & 0 deletions WEEK3/hmin27/boj5086.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>

int compare(int a, int b)
{
if (a % b == 0) return 1;
else
{
if (b % a == 0) return 2;
else return 3;
}
}
int main()
{
int num[2];
int i = 0, res;

while (1) {
scanf("%d", &num[i%2]);
if (i % 2 == 1) {
if (num[0] == 0 && num[1] == 0) break;
else
{
res = compare(num[0], num[1]);
}

switch (res) {
case 1: printf("multiple\n"); break;
case 2: printf("factor\n"); break;
case 3: printf("neither\n"); break;
}
}

i++;
}
return 0;
}
36 changes: 36 additions & 0 deletions WEEK3/hmin27/stack_queue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Stack and Queue
=====
# Stack
* Data insertion(Push)과 deletion(Pop)은 top에서만 일어남
* Last In First Out

## Implementation
1. Array-Based
- 왼쪽에서 오른쪽으로 요소를 추가
- top 요소의 index를 계속해서 저장하고 있어야 함
- Limitation
- stack의 최대 크기(배열의 크기)가 이전에 선언되어야하며 변경할 수 없음
- 가득 찬 stack에 새로운 요소를 push하려고 하면 예외가 발생함
2. Linked List
- Singly linked list 이용
- top 요소는 리스트의 첫 번째 노드에 저장됨

# Queues
* Data Insertion(enqueue or push)는 rear에서 일어나며, Data deletion(dequeue or pop)은 front에서 일어남
* First In First Out

## Implementation
1. Array-Based
- rear의 index를 계속해서 저장하고 있어야 함
- Pop: queue[0] 삭제 후 모든 요소들을 왼쪽으로 한 칸씩 이동
- Push: rear index에 저장
1-2. Improved Queue
- front와 rear의 index 모두 저장하고 있음
- Pop: front의 index 증가
- Push: rear의 index 증가
- rear이 리스트의 끝에 도달하면 모든 요소들을 왼쪽으로 이동
2. Circular Queue
- front와 rear의 index 모두 저장하고 있음
- front는 첫 요소에서 반시계 방향으로 한 칸 이동한 칸을 가리킴
- rear는 마지막 요소를 가리킴

Loading