-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds13_a.cpp
64 lines (57 loc) · 1.3 KB
/
ds13_a.cpp
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
#define MAX_NUMBER 100
#include <stdio.h>
#include <stdlib.h>
int Maxheap[MAX_NUMBER];
int pointer = 0;
void push(int number, int pointer) {
int i;
if (pointer >= MAX_NUMBER - 1) {
fprintf(stderr, "Full Max Heap\n");
exit(1);
}
i = ++pointer;
while (i != 1 && number > Maxheap[i / 2]) {
Maxheap[i] = Maxheap[i / 2];
i /= 2;
}
Maxheap[i] = number;
}
int pop(int pointer) {
if (pointer < 0) {
fprintf(stderr, "Empty Max Heap\n");
exit(1);
}
int number = Maxheap[1];
int changenumber = Maxheap[pointer--];
int parent = 1; int child = 2;
while (child <= pointer) {
if (child < pointer && Maxheap[child] < Maxheap[child + 1])
child++;
if (changenumber >= Maxheap[child]) break;
Maxheap[parent] = Maxheap[child];
parent = child; child *= 2;
}
Maxheap[parent] = changenumber;
return number;
}
int main() {
FILE* heapnumber;
fopen_s(&heapnumber, "in.txt", "r");
int heapnode;
while (!feof(heapnumber)) {
fscanf_s(heapnumber, "%d", &heapnode);
push(heapnode, pointer++);
}
fclose(heapnumber);
for (int i = 1; Maxheap[i]; i++)
printf("%d ", Maxheap[i]);
printf("\n");
printf("Á¤¼ö ÀÔ·Â : ");
scanf_s("%d", &heapnode);
for (int j = 1; j <= heapnode; j++)
int maxnumber = pop(pointer--);
for (int k = 1; k <= pointer; k++)
printf("%d ", Maxheap[k]);
printf("\n");
return 0;
}