10107  What is the Median?
Moderator: Board moderators
10107  What is the Median?
Hi! I'm getting WA with this one and it seems preety simple! is there any tricky input or something? if you know something, please post!
thanks
thanks

 Experienced poster
 Posts: 192
 Joined: Sat Nov 30, 2002 5:14 am
problem 10107
what is the wrong of this problem?
[/cpp]
Code: Select all
# include <stdio.h>
# include <math.h>
# include <ctype.h>
void main(){
long i,j,k,n,a[100],c=0;
i=0;
while(1==scanf("%ld",&a[i])){
c++;
for (k=0;k<c;k++)
for (j=k+1;j<c;j++)
if (a[i]<a[j]){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
long d=floor(c/2);
if (c%2==1){
printf ("%ld\t",a[d]);
printf("\n");
}
if (c%2==0){
int e=(a[d]+a[d1])/2;
printf ("%ld\t",e);
printf("\n");
}
i++;
}
}
We are SKARBAN.
We will make another world.
We will make another world.
you can test the sample I/O with sample from red scorpion. if you pass for that sample I/O, I think you not get WA, maybe you can get TLE or RE. RE because your array must be 10000 and your TLE because for sorting you use bubble sort. You only need 1 looping (1 for), because after input your data input have been sorted before.
 Pier
 New poster
 Posts: 38
 Joined: Thu Mar 27, 2003 9:12 pm
 Location: Aguascalientes, Mexico
 Contact:
All test OK, but still WA!
Any help appreciated!
[pascal] {$N+}
Var
m: extended;
i,j,t: longint;
n: array [1..10000] of longint;
Begin
i:= 1;
While (not eof(input)) do
begin
read(input,n);
for j:= i downto 2 do
if n[j1]>n[j] then begin
t:= n[j];
n[j]:= n[j1];
n[j1]:= t;
end
else break;
if odd(i) then writeln(output,n[(i shr 1)+1])
else begin
m:= n[(i shr 1)+1];
m:= (m + n)/2;
writeln(output,trunc(m));
end;
Inc(i);
end;
End.[/pascal]
[pascal] {$N+}
Var
m: extended;
i,j,t: longint;
n: array [1..10000] of longint;
Begin
i:= 1;
While (not eof(input)) do
begin
read(input,n);
for j:= i downto 2 do
if n[j1]>n[j] then begin
t:= n[j];
n[j]:= n[j1];
n[j1]:= t;
end
else break;
if odd(i) then writeln(output,n[(i shr 1)+1])
else begin
m:= n[(i shr 1)+1];
m:= (m + n)/2;
writeln(output,trunc(m));
end;
Inc(i);
end;
End.[/pascal]
There are 10 kind of people on this world: those who understand binary and those who don't!
No...
Each line contains ONE number only!!!
By the way, Insertion Sort is quite eough for this qq.......
Each line contains ONE number only!!!
By the way, Insertion Sort is quite eough for this qq.......
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00  16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00  16:00 (UTC)
URL: http://uva.onlinejudge.org
 Pier
 New poster
 Posts: 38
 Joined: Thu Mar 27, 2003 9:12 pm
 Location: Aguascalientes, Mexico
 Contact:
Still WA!
I changed my bubble sort for insertion sort, but I still get WA... It's deprecing to get WA so many times on a easy problem...
[pascal]
{$N+}
Var
key: real;
i,j: longint;
n: array [1..10000] of real;
Begin
i:= 1;
While (not eof(input)) do
begin
read(input,n);
key:= n; j:= i1;
While (j >=1) and (n[j] >key) do
begin
n[j+1]:= n[j];
Dec(j);
end;
n[j+1]:= key;
if odd(i) then writeln(output,round(n[(i shr 1)+1]))
else writeln(output,round((n[(i shr 1)+1] + n)/2));
Inc(i);
end;
End.
[/pascal]
[pascal]
{$N+}
Var
key: real;
i,j: longint;
n: array [1..10000] of real;
Begin
i:= 1;
While (not eof(input)) do
begin
read(input,n);
key:= n; j:= i1;
While (j >=1) and (n[j] >key) do
begin
n[j+1]:= n[j];
Dec(j);
end;
n[j+1]:= key;
if odd(i) then writeln(output,round(n[(i shr 1)+1]))
else writeln(output,round((n[(i shr 1)+1] + n)/2));
Inc(i);
end;
End.
[/pascal]
There are 10 kind of people on this world: those who understand binary and those who don't!
Pier, two obvious mistakes:
1. Why use round?! You should use trunc instead!
2. When reading the input, you should use readln instead of read. Otherwise, you might get Runtime Error!
I modified your code this way and got ACC!!! Try it!
1. Why use round?! You should use trunc instead!
2. When reading the input, you should use readln instead of read. Otherwise, you might get Runtime Error!
I modified your code this way and got ACC!!! Try it!
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00  16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00  16:00 (UTC)
URL: http://uva.onlinejudge.org
 Pier
 New poster
 Posts: 38
 Joined: Thu Mar 27, 2003 9:12 pm
 Location: Aguascalientes, Mexico
 Contact:
Hi!
Thanks a lot! I was using int:0:0 intead of round, but I changed it just to see if that helped and then forgot to change it back when I posted the question.
Also, I usually use readln instead of read but I don't understand why this makes WA! I thought they should work similar in this kind of problem.
Thanks a lot! I was using int:0:0 intead of round, but I changed it just to see if that helped and then forgot to change it back when I posted the question.
Also, I usually use readln instead of read but I don't understand why this makes WA! I thought they should work similar in this kind of problem.
There are 10 kind of people on this world: those who understand binary and those who don't!
10107 TLE
why my program gets TLE?
#include <stdio.h>
#include <stdlib.h>
long input[10050];
int sort(const void *a,const void *b){
long p =*(long *)a,q =*(long *)b;
return pq;
}
void main(){
long i=0;
while(scanf("%ld",&input)!=EOF){
i++;
qsort(input,i,sizeof(long),sort);
if(i%2==0)
printf("%ld\n",(input[i/21]+input[i/2])/2);
else
printf("%ld\n",input[(i1)/2]);
}
}
#include <stdio.h>
#include <stdlib.h>
long input[10050];
int sort(const void *a,const void *b){
long p =*(long *)a,q =*(long *)b;
return pq;
}
void main(){
long i=0;
while(scanf("%ld",&input)!=EOF){
i++;
qsort(input,i,sizeof(long),sort);
if(i%2==0)
printf("%ld\n",(input[i/21]+input[i/2])/2);
else
printf("%ld\n",input[(i1)/2]);
}
}
khobaib

 Learning poster
 Posts: 90
 Joined: Sat Feb 15, 2003 1:39 am
 Location: Paris, France
 Contact: