Update 'a2/program.c'
This commit is contained in:
parent
a6d1b6c59f
commit
3a96612e45
50
a2/program.c
50
a2/program.c
@ -0,0 +1,50 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void print(int arr[], int n, int index, int level) {
|
||||||
|
if (index < n) {
|
||||||
|
for (int i = 0; i < level; i++) {
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
printf("%d\n", arr[index]);
|
||||||
|
|
||||||
|
int left_index = 2 * index + 1;
|
||||||
|
int right_index = 2 * index + 2;
|
||||||
|
|
||||||
|
print(arr, n, left_index, level + 1);
|
||||||
|
print(arr, n, right_index, level + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int binary (int arr[], int n) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int left_index = 2 * i + 1;
|
||||||
|
int right_index = 2 * i + 2;
|
||||||
|
|
||||||
|
if (left_index < n && arr[left_index] < arr[i]) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (right_index < n && arr[right_index] < arr[i]) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int main () {
|
||||||
|
int arr[100];
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
while (scanf("%d", &arr[n]) == 1) {
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (binary(arr,n)) {
|
||||||
|
printf("Je to taka kopa:\n");
|
||||||
|
print(arr, n, 0, 0);
|
||||||
|
|
||||||
|
}else{
|
||||||
|
printf("Nie je kopa.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user