oval-and-rectangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 445 Accepted Submission(s): 198
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
Source
2018 Multi-University Training Contest 6
解析:可以参考这篇博客https://blog.csdn.net/codeswarrior/article/details/81515593
比赛时候只知道题意,根本没想到用积分做,而且这道题需要舍去第六位小数后面所有的位数,但是printf这个输出时候是按照四舍五入的舍去的,因此我又了解了下各种取整的库函数,这片博客中https://blog.csdn.net/xiamentingtao/article/details/52593648写的很详细,floor、round、ceil、fix这四种的区别,,,,这道题可以先乘以1e6,用floor函数向下取整,再除以1e6即可。
代码如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define pi acos(-1)
int main()
{
int t;
double a,b;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf",&a,&b);
double sum=(2.0*b+a*pi)*1e6;
sum=floor(sum);
printf("%.6lf\n",sum/1e6);
}
return 0;
}