您的位置:首页 > 理论基础 > 计算机网络

第十八届中山大学程序设计竞赛网络预选赛B题

2015-04-13 15:45 274 查看
Common Area

Description

Given a circle and a square whose centers coincide with each other, your task is to calculate the common area of them.

Input
The input begins with an integer T (T <= 1000), indicating the number of test cases. Each case contains two integers r and d (1 <= r, d <= 1000), indicating the radius of the circle, and the length of each side of the
square, respectively.

Output
For each case, output the common area in a line. The answer should be given with an absolute or relative error of at most 10-6.

Sample Input and Sample Output
Sample Input    Sample Output

2 3.1415926536

1 2 62.1897054604

5 8

第十八届中山大学程序设计竞赛网络预选赛的B题,一道几何体。这里注意两个地方:1,对圆与正方形交叠的三种情况的分类。2,在两种不能互相包含时,应画图表示所求面积。这里采用的方法是将重叠面积表示为四个扇形加八个直角三角形的面积和。其中扇形面积公式为0.5*θ*r*r,θ需用反三角函数求得。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double PI = 3.1415926536;

int main() {
double T, r, d;
cin >> T;
while (T--) {
cin >> r >> d;
if (2*r <= d) {
printf("%.10lf\n", PI * r * r);
}
else if (2*r > d && d*d > 2*r*r) {
double a = d*0.5;
double x = sqrt(r*r - a*a);
double s1 = r*r*0.5*(0.5*PI - 2*asin(x/r));
double s2 = 0.5*x*a;
double s = (s1+2*s2)*4;
printf("%.10lf\n", s);
}
else if (d*d <= 2*r*r) {
printf("%.10lf\n", d*d);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 几何体 ACM
相关文章推荐