hdu 1071 simpson 求积分

根据点 p1     y = a(x- x1 )^2 + c  在根据一个点 求出 a ,c。 根据 p2 p3 求出直线。

直接simpson 。



#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <cstring>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <assert.h>
#include <queue>
#define REP(i,n) for(int i=0;i<n;i++)
#define TR(i,x) for(typeof(x.begin()) i=x.begin();i!=x.end();i++)
#define ALLL(x) x.begin(),x.end()
#define SORT(x) sort(ALLL(x))
#define CLEAR(x) memset(x,0,sizeof(x))
#define FILLL(x,c) memset(x,c,sizeof(x))
using namespace std;
const double eps = 1e-9;
#define LL long long 
#define pb push_back

double a,b,c , k ;

bool bo = 1;
double F(double x){  
	if(bo ==1)
       return a*(x-b)*(x-b)+c;
    else
       return  k*x +c;
}  
  
// 三点simpson法。这里要求F是一个全局函数    
double simpson(double a,double b){  
    double c =  a+(b-a)/2;  
    return (F(a) + 4*F(c) + F(b))*(b-a)/6;  
}  
// 自适应Simpson公式(递归过程)。已知整个区间[a,b]上的三点simpson值A   
double asr(double a , double b ,double eps ,double A){  
    double c = a+ (b-a)/2;  
    double L = simpson(a,c) ,R = simpson(c,b);  
    if(fabs(A-L-R)<=15*eps) return L + R +(A-L-R)/15;  
    return asr(a,c,eps/2,L) + asr(c,b,eps/2,R);  
}  
// 自适应Simpson公式(主过程)    
double asr(double a, double b, double eps) {    
  return asr(a, b, eps, simpson(a, b));    
} 
double x[3],y[3];

void solve(){
	  b= x[0];
	  c = y[0];
	  a = (y[1] - c) / ((x[1] - b) * (x[1] - b));
	  bo = 1;
	  double ans = asr(x[1],x[2],1e-4);
	  k = (y[2] - y[1])/(x[2] - x[1]);
	  c = y[1] - k*x[1];
	  bo = 0 ;
	  double ans2 = asr(x[1],x[2],1e-4);
	  ans -= ans2;
	  printf("%.2f\n",ans);
	
}
int main(){
    int t;
    cin >>t ;
    while(t--){
    	 for(int i=0;i<=2;i++){
    	 	scanf("%lf%lf",&x[i],&y[i]);
    	 }
    	 solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值