题干
https://ac.nowcoder.com/acm/contest/17148/L
题意
平面上有两个椭圆,一个焦点在 x 轴上,一个焦点在 y 轴上,求这两个椭圆面积的并的大小,保留一位小数。
数据组数 T (1<=T<=5),椭圆与 x 轴、y 轴 的交点在区间 [-8, 8] 上。
思路
将两椭圆组合图形的横坐标分成20000份,则图形近似由20000个矩形组成。若横坐标区间长度为 2a ,则从 x==-a 开始,每次加 a/10000(即矩形的宽),x 带入椭圆公式求得矩形高,矩形面积之和即所求面积。
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e3+10;
int main()
{
int t;
scanf("%d",&t);
while(t--){
double a,b,c,d;
scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
double x, p = max(a, c)/10000.0, ans = 0.0;
if(a>=c){
x = -a;
while(x<a){
if(x>=-c&&x<=c)
ans+=p*max(sqrt(b*b*(1.0-x*x/(a*a))), sqrt(d*d*(1.0-x*x/(c*c))));
else
ans+=p*sqrt(b*b*(1.0-x*x/(a*a)));
x+=p;
}
}else{
x = -c;
while(x<c){
if(x>=-a&&x<=a)
ans+=p*max(sqrt(b*b*(1.0-x*x/(a*a))), sqrt(d*d*(1.0-x*x/(c*c))));
else
ans+=p*sqrt(d*d*(1.0-x*x/(c*c)));
x+=p;
}
}
ans*=2;
printf("%.1f\n",ans);
}
return 0;
}