oval-and-rectangle HDU - 6362(求定积分+化简)

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 :

  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 . 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
题意:

求椭圆内接矩形的周长的期望

分析:

其实就是让求均值,把所有可能周长加起来除去变化范围即可

首先要求出周长公式

我们知道椭圆的公式为

x2a2+y2b2=1 x 2 a 2 + y 2 b 2 = 1

这里写图片描述

这样我们知道y的变化范围是 [0,b] [ 0 , b ] ,并且我们可以求出x用y表示

x=a1y2b2 x = a 1 − y 2 b 2

因此可以得到周长公式

C=4(x+y)=4y+4a1y2b2 C = 4 ( x + y ) = 4 y + 4 a 1 − y 2 b 2

因为我们要求所有可能周长之和因此实际上就是求周长的黎曼和即求周长公式的积分

因此我们实际上要求 b04y+4a1y2b2dy ∫ 0 b 4 y + 4 a 1 − y 2 b 2 d y

我们当时做的时候就求到这里的,然后直接用辛普森求积分的近似公式直接做了,样例过提交死活不对,我们知道肯定是精度问题,然后又手动求导,得到式子,还是不行,就死活用三角函数代换,哎没办法基础知识不牢固,高数都忘了,反正还是太菜了

y=bsinθ y = b s i n θ

b04y+4a1y2b2dy ∫ 0 b 4 y + 4 a 1 − y 2 b 2 d y

=π20(4bsinθ+4acosθ)d(bsinθ) = ∫ 0 π 2 ( 4 b s i n θ + 4 a c o s θ ) d ( b s i n θ )

=π20(4bsinθ+4acosθ)bcosθ dθ = ∫ 0 π 2 ( 4 b s i n θ + 4 a c o s θ ) b c o s θ   d θ

=π204b2sinθcosθ+4abcos2θ dθ = ∫ 0 π 2 4 b 2 s i n θ c o s θ + 4 a b c o s 2 θ   d θ

=π202b2sin2θ+2ab(cos2θ+1)dθ = ∫ 0 π 2 2 b 2 s i n 2 θ + 2 a b ( c o s 2 θ + 1 ) d θ

=b2[cos2θ]π20+ab[sin2θ+2θ]π20 = b 2 [ − c o s 2 θ ] 0 π 2 + a b [ s i n 2 θ + 2 θ ] 0 π 2

=2b2+πab = 2 b 2 + π a b

因此周长和为 2b2+πab 2 b 2 + π a b ,因为变化范围是0-b
所以再除以b便是期望

所以最终只需求一个式子

2b+πa 2 b + π 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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值