티스토리 뷰
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include <stdio.h> #define MAX_SIZE 100 int heap[MAX_SIZE]; int heapSize = 0; void heapInit(void) { heapSize = 0; } int heapPush(int value) { if (heapSize + 1 > MAX_SIZE) { printf("queue is full!"); return 0; } heap[heapSize] = value; int current = heapSize; while (current > 0 && heap[current] > heap[(current - 1) / 2]) { int temp = heap[(current - 1) / 2]; heap[(current - 1) / 2] = heap[current]; heap[current] = temp; current = (current - 1) / 2; } heapSize = heapSize + 1; return 1; } int heapPop(int *value) { if (heapSize <= 0) { return -1; } *value = heap[0]; heapSize = heapSize - 1; heap[0] = heap[heapSize]; int current = 0; while (current * 2 + 1 < heapSize) { int child; if (current * 2 + 2 == heapSize) { child = current * 2 + 1; } else { child = heap[current * 2 + 1] > heap[current * 2 + 2] ? current * 2 + 1 : current * 2 + 2; } if (heap[current] > heap[child]) { break; } int temp = heap[current]; heap[current] = heap[child]; heap[child] = temp; current = child; } return 1; } int main(int argc, char* argv[]) { int T, N; scanf("%d", &T); for (int test_case = 1; test_case <= T; test_case++) { scanf("%d", &N); heapInit(); for (int i = 0; i < N; i++) { int value; scanf("%d", &value); heapPush(value); } printf("#%d ", test_case); for (int i = 0; i < N; i++) { int value; heapPop(&value); printf("%d ", value); } printf("\n"); } return 0; } | cs |
'digitalization' 카테고리의 다른 글
그래도 수명이 절반이 되어서는 풀이 및 코드 (1) | 2019.08.26 |
---|---|
삼성전자 sw 상시 B형(professional) 후기 - 삼성전자 용인 서천 인재개발원에서 (0) | 2019.08.24 |
삼성 코딩테스트 B형을 위한 퀵소트, 정렬 nlogn (0) | 2019.08.19 |
삼성 코딩 테스트용 양방향 링크드 리스트 linked list(스택 큐로 활용) (0) | 2019.08.19 |
2019년 5월, 삼성전자 인재개발원에서 상시sw 역량테스트 A형 후기(feat. 삼성 신입 sw역량테스트와 비교) (0) | 2019.05.11 |
댓글