oval-and-rectangle(椭圆与矩形)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41181771/article/details/81627613

题目链接
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),需要求出椭圆内接矩形的周长的期望。
看到这里,一个数学小菜鸡掉下了悔恨的眼泪…
高数真滴菜…
这里写图片描述
在图中,有 x 2 a 2 \frac{x^2}{a^2} + y 2 b 2 \frac{y^2}{b^2} =1,而y的变化范围是[0,b];
则公式可变换为:
x=a 1 y 2 b 2 \sqrt {1-\frac{y^2}{b^2}}
所以,周长为:
C=4(x+y)=4y+4a 1 y 2 b 2 \sqrt {1-\frac{y^2}{b^2}}
然后就需要求出积分:
0 b 4 y + 4 a 1 y 2 b 2 &ThinSpace; d y \int_0^b {4y+4a\sqrt {1-\frac{y^2}{b^2}}} \,{\rm d}y
结果为
=2 b 2 b^2 + π \pi ab
然后除以b以后,结果就是
=2b+ π \pi a

然后加到代码里面求解即可:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double Pi=acos(-1.0);///emmmm,就是pai嘛....
void perimeter()
{
    double a,b;
    scanf("%lf%lf",&a,&b);
    double ans=2*b+Pi*a;///也就是刚刚那个公式
    printf("%.6f\n",ans-0.0000005);///不让进位,进位结果会错的
}
int main()
{
    int t;
    cin >> t;
    while(t--)
        perimeter();
    return 0;
}

GG

猜你喜欢

转载自blog.csdn.net/qq_41181771/article/details/81627613