-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad2_ms.c
64 lines (57 loc) · 1.06 KB
/
zad2_ms.c
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
#include <stdio.h>
#include <stdlib.h>
int abs(int a)
{
if (a >= 0)
return a;
return -a;
}
int blisko_zera(int *v, int n)
{
int i = 0, j = n - 1;
int isIncreasing = v[0] < v[1];
while (i + 1 < j)
{
int cent = (i + j) / 2;
if (cent[v] == 0)
{
return 0;
}
if (isIncreasing)
{
if (v[cent] < 0)
{
i = cent;
}
else
{
j = cent - 1;
}
}
else
{
if (v[cent] > 0)
{
i = cent;
}
else
{
j = cent - 1;
}
}
}
return abs(v[j]);
}
int main()
{
int n;
if(!scanf("%d", &n)) printf("wrong input");
int *t = (int *)malloc((size_t)n * sizeof(int));
for (int i = 0; i < n; i++)
{
if(!scanf("%d", &t[i])) printf("wrong input");
}
int nearestAbsZero = blisko_zera(t, n);
printf("%d\n", nearestAbsZero);
free(t);
}