hdoj 4033(成都网络赛题)

精度。。。二分的左右边界。。。。唉。。错在这好多次呀。。。


#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
const double eps=1e-9;
const double PI = acos(-1.0);
double a[110],max1,min1,mid;
int n;
int cal(double mid){
    double sum=0;
    for(int i=1;i<=n;i++){
        if(a[i]+a[i+1]<mid+eps) return 1;
        if(fabs(a[i]-a[i+1])>mid-eps) return -1;
        sum+=acos((a[i]*a[i]+a[i+1]*a[i+1]-mid*mid)/2/a[i]/a[i+1]);
    }
    if(sum > 2*PI+eps) return 1;
    else if(sum < 2*PI-eps) return -1;
    return 0;
}
int main(){
    int cas,i,j,flag;
    scanf("%d",&cas);
    for(i=1;i<=cas;i++){
        max1=-1,flag=0;
        scanf("%d",&n);
        for(j=1;j<=n;j++){
            scanf("%lf",&a[j]);
            if(a[j]>max1) max1=a[j];
        }
        a[n+1]=a[1];
        min1=0,max1*=2,mid=(min1+max1)/2;
        while(max1-min1>eps){
            int ans=cal(mid);
            if(ans == 1){
                max1=mid,mid=(max1+min1)/2;
            }
            else if(ans == -1){
                min1=mid,mid=(max1+min1)/2;
            }else{
                flag=1;break;
            }
        }
        if(flag)
            printf("Case %d: %.3lf\n",i,mid);
        else
            printf("Case %d: impossible\n",i);
    }
    return 0;
}


你可能感兴趣的:(网络)