-
Notifications
You must be signed in to change notification settings - Fork 0
/
10107.cpp
100 lines (81 loc) · 1.41 KB
/
10107.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<stdio.h>
#include<stdlib.h>
/* What is the Median? - 10107 */
struct lista{
unsigned long info;
struct lista* prox;
};
typedef struct lista Lista;
Lista* inicializa(void){
return NULL;
}
void libera(Lista* l){
Lista* p=l;
Lista* t;
while(p!=NULL){
t = p->prox;
free(p);
p = t;
}
}
Lista* cria_no(unsigned long i){
Lista* p = (Lista*)malloc(sizeof(Lista));
p->info = i;
return p;
}
Lista* insere_ordenado(Lista* l,unsigned long i){
Lista* novo = cria_no(i);
Lista* p = l;
Lista* ant = NULL;
while(p!=NULL && p->info<i){
ant = p;
p = p->prox;
}
if(ant == NULL){
novo->prox = l;
l = novo;
}
else{
novo->prox = ant->prox;
ant->prox = novo;
}
return l;
}
main(){
int i,size=0;
unsigned long x,median;
Lista* l;
Lista* p;
l = inicializa();
while(scanf("%lu",&x)!=EOF)
{
l=insere_ordenado(l,x);
p=l;
size++;
i = 1;
median = 0;
if(size%2==0)
{
while(i<size/2)
{
p = p->prox;
i++;
}
median += p->info;
p = p->prox;
median += p->info;
median/=2;
}
else
{
while(i<(size+1)/2)
{
p = p->prox;
i++;
}
median = p->info;
}
printf("%lu\n",median);
}
libera(l);
}