1. 程式人生 > >HDU1724-辛普森積分公式法求橢圓面積

HDU1724-辛普森積分公式法求橢圓面積

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

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 2 2 1 -2 2 2 1 0 2 Sample Output 6.283 3.142 Author 威士忌 Source
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
10006793 2014-01-21 21:22:02 Accepted 1724 468MS 316K 628 B C++

思路: 這道題目的意思是:求出所給橢圓的藍色陰影部分面積。由於陰影部分是不規則圖形,所以,可以考慮用數值分析的辦法- 辛普森公式或者牛頓-科特斯公式求積分法,更多的公式見數值分析教材,辛普森的公式如下,為了增加精度,可以用二分法 迭代細化。
這是核心公式,具體實現見程式碼:(這種辦法的效率真不怎麼樣,跑了400ms)
#include <iostream>
#include <math.h>
using namespace std;

const double esp = 1e-10;
double a,b;

double f(double x)
{
	return b * sqrt(1.0-(x*x)/(a*a));
}

double simpson(double l,double r)
{
	return (f(l)+4*f((l+r)/2.0)+f(r))/6.0*(r-l);
}

double integral(double l,double r)
{
	double mid = (l+r)/2.0;
	double res = simpson(l,r);
	if (fabs(res-simpson(l,mid)-simpson(mid,r)) < esp)
		return res;
	else
		return integral(l,mid) + integral(mid,r);
}
int main()
{
	int T;
	double l,r;
	cin >> T;
	while (T--)
	{
		cin >> a >> b >> l >> r;
		printf("%.3lf\n",2*integral(l,r));
	}
	return 0;
}

相關推薦

HDU1724-積分公式橢圓面積

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submissio

hdu1724 自適應積分 面積積分

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #

【自適應積分hdu1724 Ellipse

nts sin -s res a* max this 技術 eight Ellipse Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm

積分的學習

微積分是許多學科的基礎,在程式設計方面也有很大的作用,程式裡寫積分很多時候是用梯形法,今天學了一種叫辛普森積分的方法,感覺很好用只不過辛普森積分法在區間較大時誤差比較大,所以一般用二分減小區間大小以獲得更加精確的結果。現在以求兩個圓柱體相交部分的體積為例來學習辛普森積分:給

Ellipse HDU - 1724(自適應積分

img display 一個 blank c++ can 過程 遞歸 for Ellipse HDU - 1724 emmm...快比賽了先補幾個模板~ 自適應辛普森積分 1 #include <bits/stdc++.h> 2 using

並不對勁的積分

bzoj 什麽 ++ cond script first 不用 bubuko alt 並不會計算幾何的並不對勁的人想必是非常之菜的。 很對勁的太刀流在這裏-> 辛普森積分本身是把一段函數f(x)當成二次函數算,用(f(l)+4*f((l+r)/2)+f(r))*(r-

[BZOJ1502]月下檸檬樹(自適應積分)

成了 typedef 高度 i++ ble 4.0 技術 https SQ 1502: [NOI2005]月下檸檬樹 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1387 Solved: 739[Submit][Sta

積分

urn 計算幾何 include sca pre 每次 cst scanf rac 引例 計算積分:\[\int_{L}^{R} \frac{cx+d}{ax+b} dx\] 題解 其實可以直接求導然後做,然而太弱根本推不出來. 於是就可以使用自適應辛普森積分。 辛普森(S

【BZOJ2178】圓的面積並(積分

lse double truct += ref bzoj2178 .com spa namespace 【BZOJ2178】圓的面積並(辛普森積分) 題面 BZOJ 權限題 題解 把\(f(x)\)設為\(x\)和所有圓交的線段的並的和。 然後直接上自適應辛普森積分。 我精

洛谷 P4525 & P4526 [模板] 自適應積分

題目:https://www.luogu.org/problemnew/show/P4525 https://www.luogu.org/problemnew/show/P4526 學習辛普森積分:https://blog.csdn.net/VictoryCzt/article/details/80660

hdu 1724 Ellipse —— 自適應積分

題目:http://acm.hdu.edu.cn/showproblem.php?pid=1724 函式都給出來了,可以用辛普森積分; 一開始 eps = 1e-8 TLE了,答案只要三位小數,那麼 eps = 1e-5 即可; 這次用了比較標準的寫法^_^ 程式碼如下: #include&l

hdu 1724 Ellipse——積分

題目:http://acm.hdu.edu.cn/showproblem.php?pid=1724 #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #defin

bzoj 2178: 圓的面積並 (積分

2178: 圓的面積並 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1679  Solved: 433 [Submit][Status][Discuss] Description 給出N個圓,求其面積並 Input

[LOJ2267][SDOI2017]龍與地下城-FFT-自適應積分

龍與地下城 Description 小Q同學是一個熱愛學習的人,但是他最近沉迷於各種遊戲,龍與地下城就是其中之一。 在這個遊戲中,很多場合需要通過擲骰子來產生隨機數,並由此決定角色未來的命運,因此骰子堪稱該遊戲的標誌性道具。 骰子也分為許多種類,比如

c#使用委託實現多個函式的定積分

用辛普生法對幾個不同的被積函式求指定區間的定積分。被積函式為f(x),積分割槽間[a,b]被等分為n=2k份,每份步長為h=(b-a)/n,則積分值為:S≈h((f(a)+f(b))/2 +f(a+h)+…+f(a+(n-1)h))。 執行結果:

數值積分Python(公式,Cotes公式

問題 求 Python程式碼 from numpy import exp def f(x): """被積函式e^(x^2)""" return exp(pow(x,2)) def simpson(a,b): """辛普森公式""" return (b-a)*(f(a)+

[待完善]關於公式的一點想法

16px 意思 onclick 自己 很大的 play 更多 ble turn [吐槽]   嗯一開始接觸到這個東西其實是因為某道凸包的題目好像可以用這個奇妙的方法來算   但其實了解也不是很深,只是覺得這個東西十分有意思,   所以先稍微寫一下自己的想法,了解更多之

HDU 1724 ——————自適應

Ellipse Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2916 Accepted Submission(s): 133

公式(理論+程式碼)

數學分析中的泰勒公式告訴我們,對任何函式,我們都可以用多項式函式去逼近它的函式值,且對於解析性質很好的函式(例如任意階可導+任意階導函式有界),我們可以將誤差縮小到任意小。 其中 那麼,我們在求解析式複雜或者可能無法用初等函式表示原函式的積分問題時,能否也可以用多項式函式來代替呢? 辛普

自適應公式

自適應辛普森公式 時間複雜度O(能過) double f(double x){ return x*x+sqrt(x); //積分函式 } double simpson(double a,double b){ double c=(a+b)/2.0; retu