HDU-6362 oval-and-rectangle

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轴做对称点,顺次连接此四点构成矩形,求出这些矩形周长的期望。

第一个做这种题,本来是没有思路的,后来别人说用积分,我想了想好像是那么回事。

根据椭圆公式和矩形周长计算公式你可以得出这个积分公式\int_{0}^{b}(4y+4a\sqrt{1-\frac{y^{2}}{b^{2}}} )dy,积分之后得到周长和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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Honyelchak

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值