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

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值