第十八届中山大学程序设计竞赛网络预选赛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;
}
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;
}
相关文章推荐
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 A - 成功人士
- 中南大学第十一届大学生程序设计竞赛网络预选赛总结
- “正直角三角形” Contest2074 - 中南大学第九届大学生程序设计竞赛网络预选赛
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 B - 迭代归一
- 南邮第八届程序设计竞赛之网络预选赛小结 一(错题集)
- 安徽大学第九届大学生程序设计竞赛 网络预选赛
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 C - 置换的魔术
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 D - 买买买
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 F - 周末大法好
- ACM/ICPC黑龙江省第四届大学生程序设计竞赛网络预选赛有感
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 G - 交换大法好
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 H - 今年多少岁
- 【字典树】Message Flood - 第9届中山大学程序设计竞赛预选赛
- 【字典树】Message Flood - 第9届中山大学程序设计竞赛预选赛
- 外卖的撕‘哔’大战 Contest2074 - 中南大学第九届大学生程序设计竞赛网络预选赛
- 中南大学第九届大学生程序设计竞赛网络预选赛
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A Minimum Sum 树状数组
- 2013年北京师范大学新生程序设计竞赛网络赛-- F、 Araleii & Bill的冠名权争夺战
- ACM-南京理工大学第八届程序设计竞赛-网络赛(2016.04.17)
- 北京林业大学“计蒜客”杯程序设计竞赛 网络赛 A. 喝酒