hdu 1724 Ellipse(Simpson积分法)

来源:互联网 发布:好看的恐怖电影知乎 编辑:程序博客网 时间:2024/05/22 06:32

Ellipse

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1152    Accepted Submission(s): 435


Problem Description
Math is important!! Many students failed in 2+2’s mathematical test, so let's AC this problem to mourn for our lost youth..
Look this sample picture:
 
 


A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PI*a*b )
 

Input
Input may contain multiple test cases. The first line is a positive integer N, denoting the number of test cases below. One case One line. The line will consist of a pair of integers a and b, denoting the ellipse equation , A pair of integers l and r, mean the L is (l, 0) and R is (r, 0). (-a <= l <= r <= a).
 

Output
For each case, output one line containing a float, the area of the intersection, accurate to three decimals after the decimal point.
 

Sample Input
22 1 -2 22 1 0 2
 

Sample Output
6.2833.142
 
 
 
Simpson积分法:http://zh.wikipedia.org/wiki/%E8%BE%9B%E6%99%AE%E6%A3%AE%E7%A9%8D%E5%88%86%E6%B3%95
 
 
AC代码:
#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>#include <ctime>#include <algorithm>#define ll long longusing namespace std;const int INF = 1e9;const int maxn = 505;const double eps = 1e-8;double a, b;double F(double x){    return 2.0 * b * sqrt(1.0 - x * x / (a * a));}double simpson(double l, double r){    double mid = (l + r) / 2.0;    return (r - l) * (F(l) + 4.0 * F(mid) + F(r)) / 6.0;}double work(double l, double r, double sum){    double mid = (l + r) / 2.0;    double ls = simpson(l, mid);    double rs = simpson(mid, r);    if(abs(ls + rs - sum) <= eps) return sum;    return work(l, mid, ls) + work(mid, r, rs);}int main(){    int t;    double l, r;    scanf("%d", &t);    while(t--)    {        scanf("%lf%lf%lf%lf", &a, &b, &l, &r);        double ans = work(l, r, simpson(l, r));        printf("%.3f\n", ans);    }    return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买到了坏了的水果商家不赔怎么办 小孩奶不够吃又不吃奶粉怎么办 10个月的宝宝便秘很严重怎么办 四岁的宝宝突然不怎么吃饭怎么办 1岁7个月宝宝突然不爱吃饭怎么办 宝宝发烧好了之后不吃辅食怎么办 吃母乳的宝宝不喝奶粉怎么办 奶水不足宝宝又不喝奶粉怎么办 6个月宝宝断奶哭闹不用奶瓶怎么办 小孩不爱吃饭怎么办该吃些什么 二个月宝宝只认母乳不喝牛奶怎么办 满月宝宝只认奶粉不认母乳怎么办 宝宝四个月只认奶瓶不认母乳怎么办 新生儿只认奶瓶不认母乳怎么办 7个月宝宝不吃辅食怎么办 8个月小孩不吃辅食怎么办 9个月小孩不吃辅食怎么办 十一个月宝宝拉肚子拉水怎么办 小孩好动上课也坐不住会怎么办 幼儿大班关于环保的手抄报怎么办 捷达车打开丝位子风扇总转怎么办 小班个案分析:好动的孩子该怎么办 孩子突然过于的兴奋不睡觉是怎么办 刚出生的宝宝听力未通过怎么办 孩子多动症被老师打不上学怎么办 小孩在校被学生打家长该怎么办? 8岁儿童好动上课不注意听课怎么办 怀孕第一个月喝了啤酒怎么办 不知道自己怀孕喝了酒怎么办 怀孕一个月不知道喝酒了怎么办 老师说孩子上课不专心听讲怎么办 二年级孩子上课不认真听讲怎么办 二年级的孩子浮躁上课不认真怎么办 三年级的小孩上课不认真听讲怎么办 大班孩子上课不专心听讲玩怎么办 老师跟家长说小孩上课讲话怎么办 别的家长动手打自己的孩子怎么办? 一岁3个月宝宝骨龄偏小怎么办 宝宝9个月了越来越粘人怎么办? 孩子突然说话结巴口吃了怎么办啊 很久不说话了不敢说话了怎么办