Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

怎么一回事,负值也会丢失精度????

Posted by qywyh_scut at 2006-02-21 19:57:31 on Problem 2546
ac程序:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
	//const double pi = 3.141592653589793;
	double pi=asin(1)*2;
	double x1,y1,r1,x2,y2,r2;
	double cosb,cosa;
	double s,area;
	double d2;
	while (cin>>x1>>y1>>r1>>x2>>y2>>r2)
	{
    if (r1<r2)
	{
		d2 = r1;
		r1 = r2;
		r2 = d2;
	}
    d2 = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	if (r1+r2<=d2)
	{
		area = 0;
		cout<<setiosflags(ios::fixed)<<setprecision(3)<<area<<endl;
		//break;
	}
	if (r1-r2>=d2)
	{
		area = pi*r2*r2;
		cout<<setiosflags(ios::fixed)<<setprecision(3)<<area<<endl;
		//break;
	}
	
	if (r1+r2>d2&&r1-r2<d2)
	{
        cosb = acos((r1*r1+d2*d2-r2*r2)/2/r1/d2);
		cosa = acos((r2*r2+d2*d2-r1*r1)/2/r2/d2);
		s = (r1+r2+d2)/2;
		//cout<<s<<endl;
		area = sqrt((s-r2)*(s-r1)*(s-d2)*s);
		//cout<<((s-a)*(s-b)*(s-c)*s)<<endl;
		area = r2*r2*cosa+r1*r1*cosb-area*2;
		//area = floor(area*1000+0.5)/1000;
        cout<<setiosflags(ios::fixed)<<setprecision(3)<<area<<endl;
	}
	}
	return 0;
}

WA程序:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
	//const double pi = 3.141592653589793;
	double pi=asin(1)*2;
	double x1,y1,r1,x2,y2,r2;
	double a,b,c,cosb,cosa;
	double s,area;
	double d2;
	while (cin>>x1>>y1>>r1>>x2>>y2>>r2)
	{
    if (r1<r2)
	{
		d2 = r1;
		r1 = r2;
		r2 = d2;
	}
    d2 = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	if (r1+r2<=d2)
	{
		cout<<setiosflags(ios::fixed)<<setprecision(3)<<0<<endl;
	}
	if (r1-r2>=d2)
	{
		
		cout<<setiosflags(ios::fixed)<<setprecision(3)<<pi*r2*r2<<endl;
	}
	
	if (r1+r2>d2&&fabs(r1-r2)<d2)
	{
		a = r1;
		b = r2;
		c = d2;
        cosb = acos((a*a+c*c-b*b)/2/a/c);
		cosa = acos((b*b+c*c-a*a)/2/b/c);
		s = (a+b+c)/2;
		//cout<<s<<endl;
		area = sqrt((s-a)*(s-b)*(s-c)*s);
		//cout<<((s-a)*(s-b)*(s-c)*s)<<endl;
		area = b*b*cosa+a*a*cosb-area*2;
		//area = floor(area*1000+0.5)/1000;
        cout<<setiosflags(ios::fixed)<<setprecision(3)<<area<<endl;
	}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator