精度问题怎么解决(玄2关
查看原帖
精度问题怎么解决(玄2关
525038
leiwusi楼主2024/3/10 21:19

rt,每次大概差3~4左右

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define double long double
using namespace std;

const int MAXN = 1e5+114;
const double eps=1e-10;
struct nod{
    double x,y;
} ls[MAXN],pt;
double r = 0;
int n;
double dis(nod a,nod b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void tpcir(nod a,nod b){
    pt.x = (a.x+b.x)/2;
    pt.y = (a.y+b.y)/2;
    r = dis(pt,b);
}

void thpcir(nod p1,nod p2,nod p3){
    double a,b,c,d,e,f;
    a=p2.y-p1.y;
    b=p3.y-p1.y;
    c=p2.x-p1.x;
    d=p3.x-p1.x;
    f=p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y;
    e=p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y;
    pt.x=(a*f-b*e)/(2*a*d-2*b*c);
    pt.y=(d*e-c*f)/(2*a*d-2*b*c);
    r = dis(pt,p1);
}

int main(){
    cin>>n;
    for(int i = 1;i<=n;i++) cin>>ls[i].x>>ls[i].y;
    random_shuffle(ls+1,ls+n+1);
    

    pt = ls[1];
    r = 0;
    for(int i = 2;i<=n;i++){
        if(dis(pt,ls[i])>eps+r){
            r = 0;pt = ls[i];
            for(int j = 1;j<=i-1;j++){
                if(dis(pt,ls[j])>eps+r){
                    tpcir(pt,ls[j]);
                    for(int k = 1;k<=j-1;k++){
                        if(dis(pt,ls[k])>eps+r){
                            thpcir(ls[i],ls[j],ls[k]);
                        }
                    }
                }

            }
        }
    }

    printf("%.10Lf\n%.10Lf %.10Lf\n", r, pt.x, pt.y);

}

2024/3/10 21:19
加载中...