oval-and-rectangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1200 Accepted Submission(s): 520
Problem Description
Patrick Star find an oval.
The half of longer axes is on the x-axis with length a.
The half of shorter axes is on the y-axis with length b.
Patrick Star plan to choose a real number c randomly from [0,b], after that, Patrick Star will get a rectangle :
1. The four vertexes of it are on the outline of the oval.
2. The two sides of it parallel to coordinate axis.
3. One of its side is y=c.
Patrick Star want to know the expectations of the rectangle's perimeter.
Input
The first line contain a integer T (no morn than 10), the following is T test case, for each test case :
Each line contains contains two integer a, b (0<b<a<105). Separated by an white space.
Output
For each test case output one line denotes the expectations of the rectangle's perimeter .
You should keep exactly 6 decimal digits and ignore the remain decimal digits.
It is guaranted that the 7-th decimal digit of answer wont be 0 or 9.
Sample Input
1 2 1
Sample Output
8.283185
思路
题意是给定一个椭圆标准方程的a,b(椭圆的长半轴长和短半轴长),在【0,b】内取一个数,则过点(0,b)且平行于x轴的直线与椭圆交于两点,再将此两点关于x轴做对称点,顺次连接此四点构成矩形,求出这些矩形周长的期望。
第一个做这种题,本来是没有思路的,后来别人说用积分,我想了想好像是那么回事。
根据椭圆公式和矩形周长计算公式你可以得出这个积分公式,积分之后得到周长和a*b*π+2*b*b,然后除以b就得到了期望。
做到这个地方还没完,题上说保留到小数点后六位,但不是四舍五入的方式。
%.6lf 为四舍五入的方式,不能用。
在这里我采用了将小数乘1000000,然后向下取整,再除以1000000。
网上其他的做法:-0.0000005。太妙了。
代码
#include<cstdio>
#include<cmath>
using namespace std;
const double PI = acos(-1.0);
int main(){
int t;
scanf("%d",&t);
while(t--){
int a,b;
scanf("%d%d",&a,&b);
double c = 2*b+PI*a;
c *= 1000000;
c = floor(c)/1000000;
printf("%.6lf\n",c);
}
return 0;
}