1
This commit is contained in:
parent
c11006e42b
commit
501ff379f0
66
a2/program.c
Normal file
66
a2/program.c
Normal file
@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
int is_min_heap(int *a, int n) {
|
||||
for (int i = 0; i <= (n - 2) / 2; i++) {
|
||||
int l = 2 * i + 1;
|
||||
int r = 2 * i + 2;
|
||||
|
||||
if (l < n && a[i] > a[l]) return 0;
|
||||
if (r < n && a[i] > a[r]) return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void preorder(int *a, int n, int i, int level) {
|
||||
if (i >= n) return;
|
||||
|
||||
printf("%*s%d\n", level, "", a[i]);
|
||||
preorder(a, n, 2*i+1, level+1);
|
||||
preorder(a, n, 2*i+2, level+1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
char line[10000];
|
||||
int numbers[2000];
|
||||
int count = 0;
|
||||
|
||||
|
||||
if (!fgets(line, sizeof(line), stdin)) {
|
||||
printf("Nie je kopa.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int i = 0;
|
||||
while (line[i] != '\0') {
|
||||
|
||||
while (isspace(line[i])) i++;
|
||||
|
||||
|
||||
if (!(isdigit(line[i]) || line[i] == '-' || line[i] == '+'))
|
||||
break;
|
||||
|
||||
int sign = 1;
|
||||
if (line[i] == '-') { sign = -1; i++; }
|
||||
else if (line[i] == '+') { i++; }
|
||||
|
||||
long value = 0;
|
||||
while (isdigit(line[i])) {
|
||||
value = value * 10 + (line[i] - '0');
|
||||
i++;
|
||||
}
|
||||
|
||||
numbers[count++] = (int)(value * sign);
|
||||
}
|
||||
|
||||
if (is_min_heap(numbers, count)) {
|
||||
printf("Je to taka kopa:\n");
|
||||
preorder(numbers, count, 0, 0);
|
||||
} else {
|
||||
printf("Nie je kopa.\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user