10432 - Polygon Inside A Circle

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

amd-RS
New poster
Posts: 27
Joined: Thu Sep 05, 2002 7:37 am

10432 - Polygon Inside A Circle

Post by amd-RS »

What's wrong with my code ?

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

#define Pi 3.1415926535897932384626433832795029L


int main()
{
long double ang;
int r, n;

while(scanf("%d %d",&r,&n)==2)
{
ang = 180 - ((double)180*(n-2)/n);
printf("%.3lf\n",(sin(Pi/180*ang)*r*r*n)/2);
}
return 0;
}

[/c]

Can you see ? Thanks, Aur
Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum »

I think this question might suffer from accuracy errors ? Try at the largest values of n and r, for
20000 20000
my program produces 1256637040.765 although my program is WA. However this value does seem accurate when I compute the value to 40 decimals in Maple my program is agreeing to 5 dp :(
PdR
New poster
Posts: 24
Joined: Mon Dec 30, 2002 4:27 am

Post by PdR »

Caesum wrote:I think this question might suffer from accuracy errors ?
Probably, althought i got it right at first try.

I used something like
[c]#ifndef PI
#define PI (2*acos(0))
#endif[/c]
to define the value of PI.
Maybe this is your source of trouble.
Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum »

and r should be double
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

I also read them in as doubles, so maybe that'll work?
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

Code: Select all

(sin(Pi/180*ang)*r*r*n)/2
should be:

Code: Select all

(sin(Pi/180*ang)*r*r*n)/2.0
and I use

Code: Select all

sin(acos(-1.0)*2.0/n)
maybe its help us ....

Dominik Michniewski
amd-RS
New poster
Posts: 27
Joined: Thu Sep 05, 2002 7:37 am

Post by amd-RS »

Thanks a lot, I've follow the suggestions and get accepted now :D

But I have a question ! Why ACM judge doesn't define M_PI ? I tried it and get compiler error !

Aur
rjhadley
Learning poster
Posts: 73
Joined: Mon Oct 14, 2002 7:15 am
Location: United States

Post by rjhadley »

M_PI is not part of the ANSI standard and the judge uses the "-ansi" compilation switch.
Faizur
New poster
Posts: 39
Joined: Fri Jun 06, 2003 3:04 pm

10432 wa????

Post by Faizur »

Can any one tell me why i am getting wrong answer with the following code????????

Code: Select all

#include<stdio.h>
#include<math.h>
int main()
{
	double r,n,ac,p,pi,t,q;
	while(scanf("%lf%lf",&r,&n)==2)
	{
		pi=2*acos(0);
		ac=pi*r*r;
		p=2*pi*r;
		p/=n;
		t=0.5*r*r*pi/n;
		q=(0.5*r*r*sin(pi/n));
		t-=q;
		ac-=t*n;
		printf("%.3lf\n",ac);

	}
}
hager
New poster
Posts: 10
Joined: Wed Jan 01, 2003 4:26 am
Location: Ume

Post by hager »

The reason is probably due to precision errors when n is large. What happens then is that t becomes so small in comparison to ac so that subtracting t*n from ac will have no effect. Anyway, there's a simpler solution to this problem, and you have the answer (almost) somewhere in your code, but I'll leave it to you to sort out what you need :)

Best Regards
raymond85
New poster
Posts: 21
Joined: Tue Jul 01, 2003 9:26 am
Location: Hong Kong
Contact:

10432

Post by raymond85 »

I got WA and i figured out that it should be caused by some precision error while I was defining the value of Pi.
I am using pascal to write the program and the built in sine function requires a radian value instead of degree. So I have to convert it back to radian first and therefore I requires Pi. Is there anyways to define the exact value of Pi? (or perhaps, at least it's precious enough for me to get an AC)

Thx! Sorry for my poor English.
Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

The value of pi defined in the system unit is more than enough.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
raymond85
New poster
Posts: 21
Joined: Tue Jul 01, 2003 9:26 am
Location: Hong Kong
Contact:

Post by raymond85 »

thx
I got AC in the problem!
thx for telling
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

10432 - Angle conversion

Post by Joseph Kurniawan »

Can anybody tell me how to convert angle in degree to angle in radians ?
For example if the angle is 60 degree, what would it be in radians? Please show me the proccess of the conversion.
Thanx in advance.
Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

That's not hard.

First you need to know that pi rad = 180 degrees
Thus 1 degree = pi / 180 radians, or n degrees = n * pi / 180 rad.

e.g. 60 degrees = 60 * pi / 180 = pi / 3 rad.

Hope this helps! :wink:
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Post Reply

Return to “Volume 104 (10400-10499)”