hdu oval-and-rectangle
oval-and-rectangle
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 4
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 < 10^5$). 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
Source
2018 Multi-University Training Contest 6
其實就是讓求均值,把所有可能周長加起來除去變化範圍即可
首先要求出周長公式
我們都知道橢圓的公式
我們知道y的變化範圍是[0,b][0,b],並且我們可以求出x用y表示
因為題目要求保留6位小數,並且後面的全部捨去,因此為了防止%.6f造成四捨五入,因此給答案-0.0000005
#include<bits/stdc++.h>
using namespace std;
#define PI acos(-1)
int main()
{
int t,a,b;
double s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
s=a*PI+2*b-0.0000005;
printf("%.6lf\n",s);
}
}