这场比赛充分暴露了我是学渣的事实
转载地址:https://blog.csdn.net/codeswarrior/article/details/81515593
oval-and-rectangle
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 :
-
The four vertexes of it are on the outline of the oval.
-
The two sides of it parallel to coordinate axis.
-
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 . 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.
- 1
- 2
- 3
- 4
Sample Input
1
2 1
- 1
- 2
- 3
Sample Output
8.283185
- 1
- 2
题意:
求椭圆内接矩形的周长的期望
分析:
其实就是让求均值,把所有可能周长加起来除去变化范围即可
首先要求出周长公式
我们知道椭圆的公式为
x2a2+y2b2=1x2a2+y2b2=1
这样我们知道y的变化范围是[0,b][0,b],并且我们可以求出x用y表示
x=a1−y2b2−−−−−√x=a1−y2b2
因此可以得到周长公式
C=4(x+y)=4y+4a1−y2b2−−−−−√C=4(x+y)=4y+4a1−y2b2
因为我们要求所有可能周长之和因此实际上就是求周长的黎曼和即求周长公式的积分
因此我们实际上要求∫b04y+4a1−y2b2−−−−−√dy∫0b4y+4a1−y2b2dy
我们当时做的时候就求到这里的,然后直接用辛普森求积分的近似公式直接做了,样例过提交死活不对,我们知道肯定是精度问题,然后又手动求导,得到式子,还是不行,就死活用三角函数代换,哎没办法基础知识不牢固,高数都忘了,反正还是太菜了
设y=bsinθy=bsinθ
∫b04y+4a1−y2b2−−−−−√dy∫0b4y+4a1−y2b2dy
=∫π20(4bsinθ+4acosθ)d(bsinθ)=∫0π2(4bsinθ+4acosθ)d(bsinθ)
=∫π20(4bsinθ+4acosθ)bcosθ dθ=∫0π2(4bsinθ+4acosθ)bcosθ dθ
=∫π204b2sinθcosθ+4abcos2θ dθ=∫0π24b2sinθcosθ+4abcos2θ dθ
=∫π202b2sin2θ+2ab(cos2θ+1)dθ=∫0π22b2sin2θ+2ab(cos2θ+1)dθ
=b2[−cos2θ]π20+ab[sin2θ+2θ]π20=b2[−cos2θ]0π2+ab[sin2θ+2θ]0π2
=2b2+πab=2b2+πab
因此周长和为2b2+πab2b2+πab,因为变化范围是0-b
所以再除以b便是期望
所以最终只需求一个式子
2b+πa2b+πa
因为题目要求保留6位小数,并且后面的全部舍去,因此为了防止%.6f造成四舍五入,因此给答案-0.0000005
code:
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int main(){
int t;
scanf("%d",&t);
while(t--){
double a,b;
scanf("%lf%lf",&a,&b);
double ans = 2 * b + pi * a ;
printf("%.6f\n",ans-0.0000005);
}
return 0;
}