## 10347 - Medians

Moderator: Board moderators

Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia

### 10347 - Medians

I have no idea why my code gets WA

I know this simple formulas:

S=(4/3)*(m(m-ma)(m-mb)(m-mc))^(1/2) , where m=(ma+mb+mc)/2;

a=(2/3)*(2mb^2+2mc^2-ma^2)^(1/2);

Am I wrong?
But my program is wrong

[pascal]Program p10347;

Const Eps = 1e-10;

Var m1,m2,m3,m,a,b,c : Extended;

begin
While Not Eof do begin
While Eoln do begin
if Eof then Break;
end;
m:=(m1+m2+m3)/2;
if 2*m1*m1+2*m2*m2-m3*m3>-Eps then a:=2/3*sqrt(Abs(2*m1*m1+2*m2*m2-m3*m3)) else a:=-1;
if 2*m2*m2+2*m3*m3-m1*m1>-Eps then b:=2/3*sqrt(Abs(2*m2*m2+2*m3*m3-m1*m1)) else b:=-1;
if 2*m3*m3+2*m1*m1-m2*m2>-Eps then c:=2/3*sqrt(Abs(2*m3*m3+2*m1*m1-m2*m2)) else c:=-1;
if (a<-0.5) or (b<-0.5) or (c<-0.5) or
(m1<-Eps) or (m2<-Eps) or (m3<-Eps) or
(a+b<c) or (a+c<b) or (b+c<a) then
Writeln('-1')
else
Writeln(4/3*sqrt(m*(m-m1)*(m-m2)*(m-m3)):0:3);
end;
end.[/pascal]

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
I would like to know if the values should be rounded up like it is said in the problem description (but if I do it I get for the sample input 5.197). And what should be printed if the triangle is not a valid one, -1 or -1.000?
I think my values should be precise enough, I used:
area = sqrt((ma+mb+mc)*(mb+mc-ma)*(ma+mc-mb)*(ma+mb-mc))/3.0.

ithamar
Learning poster
Posts: 56
Joined: Mon May 13, 2002 11:58 pm
Location: Venezuela
I dont know what's wrong with this problem. Only one person get accepted. This is not normal. Besides i resolve this problem in the last contest and i get accepted in the contest. I submmited the exact same code here and i get WA. .

Anybody knows what's happening ....

Help apreciated.

AlexandreN
New poster
Posts: 27
Joined: Sun Jul 07, 2002 6:46 pm
Location: Campina Grande - Brazil
Contact:

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
Thanks for your reply, a few moments ago I read what has changed after the contest. In the case the area is 0 one should output -1.000. I got it Accepted.

Cucu
New poster
Posts: 4
Joined: Wed Nov 20, 2002 9:43 pm

### Always WA!!!

Hi, I think that the program gives the rigth answer but I get wrong answer, I've tried many times changing subtilities in the presentation, but i coudn't get it accepted.
Heeelppp!!!

[c]
#include <stdio.h>
#include <math.h>

double area (double m1, double m2, double m3)
{
double a,b,c, aux,x, y, z;
a=m1*2.0/3.0;
b=m2*2.0/3.0;
c=m3/3.0;
aux=(a*a+b*b-2*c*c)/2;
if (aux <= 0) return -1;
z=sqrt (aux);
y=(b*b-a*a)/(4*z);
aux=c*c-y*y;
if (aux<=0) return -1;
x=sqrt (aux);
return x*z*3;
}

int main ()
{
char s [100], *pp;
float m1, m2, m3,a;
while (1) {
pp= fgets (s,1000,stdin);
if (pp==NULL) return 0;
if (strlen (s) < 4) return -1;
m1=m2=m3=-1;
sscanf (pp, "%f %f %f",&m1, &m2, &m3);
a=area (m1,m2,m3);
if (a < 0.0000000001) a=-1;
printf ("%.3lf\n",a);
}
return 0;
}
[/c]

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:
Don't know if this will solve your problem, but when I used float, it seemed to be not precise enough, and when I switched to doubles, it got AC the first time around.. so try that?

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
pay attention to value of area....
If I correct remember - area must be strictly positive (not zero). and think, in which cases you must print -1 (if area doesn't exist). think too about how you should output -1 value

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

CodeMaker
Experienced poster
Posts: 183
Joined: Thu Nov 11, 2004 12:35 pm

### 10347

Hi, why I get output limit exceeded?

Code: Select all

``````#include<stdio.h>
#include<math.h>

void main()
{
int x,y,z;
double s,v;

while(scanf("%d%d%d",&x,&y,&z)!=EOF)
{
if(x<=0 || y<=0 || z<=0)
{
printf("-1.000\n");
continue;
}

s=(x+y+z)*0.5;
v=s*(s-x)*(s-y)*(s-z);
if(v<=0)
{
printf("-1.000\n");
continue;
}
s=sqrt(v);
s*=(4.0/3.0);
printf("%.3lf\n",s);
}
}``````
Jalal : AIUB SPARKS

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

### Re: Why output limit exceeded in 10347(median)

CodeMaker wrote:Hi, why I get output limit exceeded?

Code: Select all

``````#include<stdio.h>
#include<math.h>

void main()
{
int x,y,z;
double s,v;

while(scanf("%d%d%d",&x,&y,&z)!=EOF)
/* ... */
``````
after the last number you should expect some whitespace and so probably scanf never returns EOF.

Code: Select all

``while(scanf("%d%d%d",&x,&y,&z)==3)``
will probably fix it.

Ciao!!!

Claudio

ibrahim
Experienced poster
Posts: 149
Joined: Mon Feb 07, 2005 10:28 pm
Contact:

### 10347 Need I/0

I really need this.

Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria
1. How do you check if the three values/lengths for
the MEDIANS are valid
( the case in which you should calculate the area and print it ) ?

2. If the three values given are not valid how do you print -1 ?
You should print it is -1.000.

I think one of these two is your problem.

Fakhrul Abedin
New poster
Posts: 4
Joined: Sat Apr 02, 2005 3:10 pm

### 10347 Medians WA!

I got WA for several times Can any body fix my error?

Code: Select all

``````#include<math.h>
#include<stdio.h>

main(){
double a,b,c,s;
double area,ara;
while(scanf("%f%f%f",&a,&b,&c)!=EOF){

s=(a+b+c)/2.0;
ara=s*(s-a)*(s-b)*(s-c);

if(a<0||b<0||c<0)        area=-1.0;
else if(ara<=0)		area=-1.0;
else{
area=sqrt(ara);
area=4.0*area/3.0;
}
printf("%.3lf\n",area);
}/*Input while end*/
}
``````
Thanking u for help.

I've got AC just now!
What I do are following:
1. First I take a,b,c,s as float later I take double.
2. but I use "%f" instead of "%lf", so be carefull for such silly mistake!
3.If area is even 0 print -1.
4. And always print -1 as -1.000

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
This one is just plain silly - avoid integer arithmetic! Sigh... I should learn how to read...

Yes, use doubles for everything. (worked for Java too)

Darko