a2
This commit is contained in:
parent
55c79d92df
commit
8bf4c7c4d2
67
a2/program.c
Normal file
67
a2/program.c
Normal file
@ -0,0 +1,67 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX 1000
|
||||
|
||||
// preorder vypis s odsadenim podla urovne
|
||||
void print_preorder(int *a, int n, int i, int level) {
|
||||
if (i >= n) return;
|
||||
|
||||
// odsadenie medzerami
|
||||
for (int k = 0; k < level; k++) printf(" ");
|
||||
|
||||
printf("%d\n", a[i]);
|
||||
|
||||
// lavy
|
||||
print_preorder(a, n, 2*i + 1, level + 1);
|
||||
// pravy
|
||||
print_preorder(a, n, 2*i + 2, level + 1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
char input[5000];
|
||||
int heap[MAX];
|
||||
int count = 0;
|
||||
|
||||
// nacitanie riadka
|
||||
if (!fgets(input, sizeof(input), stdin)) {
|
||||
printf("Nie je kopa.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// parsovanie cisel zo vstupu
|
||||
char *ptr = input;
|
||||
while (*ptr) {
|
||||
while (*ptr && isspace((unsigned char)*ptr)) ptr++;
|
||||
if (*ptr == '\0') break;
|
||||
|
||||
char *end;
|
||||
long val = strtol(ptr, &end, 10);
|
||||
if (ptr == end) break;
|
||||
|
||||
heap[count++] = (int)val;
|
||||
ptr = end;
|
||||
}
|
||||
|
||||
// overenie minimalnej kopovitosti
|
||||
for (int i = 0; i < count; i++) {
|
||||
int l = 2*i + 1;
|
||||
int r = 2*i + 2;
|
||||
|
||||
if (l < count && heap[l] < heap[i]) {
|
||||
printf("Nie je kopa.\n");
|
||||
return 0;
|
||||
}
|
||||
if (r < count && heap[r] < heap[i]) {
|
||||
printf("Nie je kopa.\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Je to taka kopa:\n");
|
||||
print_preorder(heap, count, 0, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user