Skip to content

Commit 5e1abeb

Browse files
authored
Update Extra.c
1 parent 692b582 commit 5e1abeb

File tree

1 file changed

+16
-40
lines changed
  • Pontos Extras/Monitor - Ponto Extra

1 file changed

+16
-40
lines changed
Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,54 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
int cmp(const void *a, const void *b)
5-
{
6-
return (*(int*)a - *(int*)b);
7-
}
4+
int cmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); }
85
//////////////////////////////////////////////////////
9-
int buscaBinaria(int *vetor, int n, int num)
10-
{
6+
int buscaBinaria(int *vetor, int n, int num) {
117
int ini = 0, fim = n - 1, meio;
128
while (ini <= fim) {
13-
meio = (ini + fim) / 2;
14-
if (vetor[meio] == num)
15-
{
9+
meio = ini + ((fim - ini) >> 1);
10+
if (vetor[meio] == num) {
1611
return meio;
17-
} else if (vetor[meio] < num)
18-
{
12+
} else if (vetor[meio] < num) {
1913
ini = meio + 1;
2014
} else
21-
{
2215
fim = meio - 1;
23-
}
2416
}
2517
return -1;
2618
}
2719
//////////////////////////////////////////////////////
28-
int main(void)
29-
{
20+
int main(void) {
3021
int caixas, x, y, i, j, num, k, lotes[10001], posicao_inicial, posicao_final, n;
3122

32-
while (scanf("%d", &caixas) != EOF)
33-
{
23+
while (scanf("%d", &caixas) != EOF) {
3424
n = 0;
35-
for (i = 0; i < 10001; i++)
36-
{
37-
lotes[i] = 0;
38-
}
3925

40-
for (i = 0; i < caixas; i++)
41-
{
26+
memset(lotes, 0, sizeof(lotes));
27+
28+
for (i = 0; i < caixas; i++) {
4229
scanf("%d %d", &x, &y);
43-
for (j = x; j <= y; j++)
44-
{
45-
lotes[n++] = j;
46-
}
30+
for (j = x; j <= y; j++) lotes[n++] = j;
4731
}
4832

4933
qsort(lotes, n, sizeof(int), cmp);
5034

5135
scanf("%d", &num);
5236

53-
if (buscaBinaria(lotes, n, num) == -1)
54-
{
37+
if (buscaBinaria(lotes, n, num) == -1) {
5538
printf("%d not found\n", num);
5639
} else {
5740
posicao_inicial = 0;
58-
for (k = 0; k < n && lotes[k] <= num; k++)
59-
{
60-
if (lotes[k] == num)
61-
{
41+
for (k = 0; k < n && lotes[k] <= num; k++) {
42+
if (lotes[k] == num) {
6243
posicao_inicial = k;
6344
break;
64-
} else {
45+
} else
6546
posicao_inicial += lotes[k] != lotes[k+1];
66-
}
6747
}
6848
posicao_final = posicao_inicial;
69-
while (posicao_final < n - 1 && lotes[posicao_final + 1] == num)
70-
{
71-
posicao_final++;
72-
}
49+
while (posicao_final < n - 1 && lotes[posicao_final + 1] == num) posicao_final++;
7350
printf("%d found from %d to %d\n", num, posicao_inicial, posicao_final);
7451
}
7552
}
76-
7753
return 0;
7854
}

0 commit comments

Comments
 (0)