summer competition D 一些数学值的求法
题目:
Problem D GrassLand
Description
Farmer John has a grass land, which is very large that when you stand in the middle of it, you can not see its edge.
Farmer John had built his barn in the center of the grass land, which is rectangle shape of w × h. And now in the barn lives his favourate cow Bessie.
One day, farmer John goes to the market and ties Bessie to one corner of the barn with a rope of length r. Therefore Bessie can wander around the grass land as long as the rope allows her reach that point, meanwhile the rope may not pass through the barn.
Input
The first line contains a number T for the number of test cases. Then T cases follows. For each case there are three real numbers w, h, r (0 < w, h, r ≤ 1000) on one line.
Output
Output one real number on one line for each test case, the total area of grass land that Bessie can reach. You may assume that grass land is large enough that Bessie will not get out of the grass land. Round and keep three digitals after the decimal point.
Example
|
SampleInput |
SampleOutput |
|
4 2 3 2 2 3 3 2 3 5 2 3 6 |
9.425 21.991 69.115 103.540 |
这道题比较纠结,初看之下貌似容易,可细究起来,就会发现要分好几种情况讨论,而且有一种要求重合部分面积的最麻烦。
并且 边编写边学会了集中数学的函数:
海伦公式:已知三角形的三条边,求三角形的面积:
q=(a+b+c)/2;
s=sqrt(q*(q-a)*(q-b)*(q-c));
圆周率的表示方法:
PAI=acos(-1);
弧度数的求法:
cos a 中的a
a=acos(cos a);
代码:
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<math.h>
using namespace std;
int main(){
// fropen("D.in","r",stdin);
// fropen("Dout.out","w",stdout);
int m;
cin>>m;
while(m--){
double n1,n2,r;
double area=0;
double pa=acos(-1);
// cout<<pa<<endl;
cin>>n1>>n2>>r;
double longer=(n1>n2)?n1:n2;
double shorter=(n1>n2)?n2:n1;
// cout<<longer<<" "<<shorter<<endl;
if(r<=shorter){
area=(3.0/4.0)*(pa*r*r);
}
else if((r>shorter)&&(r<=longer)){
area=((3.0/4.0)*r*r*pa)+((1.0/4.0)*pow((r-shorter),2)*pa);
}
else if((r>longer)&&(r<=longer+shorter)){
area=((3.0/4.0)*r*r*pa)+((1.0/4.0)*pow((r-shorter),2)*pa)+((1.0/4.0)*pow((r-longer),2)*pa);
}
else if(r>(longer+shorter)){ //面积有重合的时候
double area1=0;
double a1=r-longer;
double b1=r-shorter;
double c1=sqrt(pow(longer,2)+pow(shorter,2));
// cout<<a1<<" "<<b1<<" "<<c1<<endl;
double temp1=(a1+b1+c1)/2;
area1=sqrt(temp1*(temp1-a1)*(temp1-b1)*(temp1-c1));
// cout<<area1<<endl;
area1=area1-((longer*shorter)/2);
double re1,re2;
re1=pa-(atan(longer/shorter)+acos((pow(c1,2)+pow(b1,2)-pow(a1,2))/(2*c1*b1)));
re2=pa-(atan(shorter/longer)+acos((pow(c1,2)+pow(a1,2)-pow(b1,2))/(2*c1*a1)));
double area2=(re1/(2))*b1*b1+(re2/(2))*a1*a1;
area=area1+area2+(3.0/4.0)*pa*r*r;
}
cout<<setiosflags(ios::fixed);
cout<<setprecision(3)<<area<<endl;
// printf("%.3f\n",area);
}
return 0;
}
浙公网安备 33010602011771号