1. 程式人生 > >Wannafly挑戰賽25 B面積並(簡單幾何)

Wannafly挑戰賽25 B面積並(簡單幾何)

題意:連結:https://www.nowcoder.com/acm/contest/197/B
來源:牛客網

cxt有一個正n邊形,它的外接圓的圓心位於原點,半徑為l。以原點為圓心,r為半徑作一個圓,求圓和這個正n邊形的面積並。

思路:一開始理解錯題意了,以為求重疊的部分,其實是求圓和多邊形並起來的面積,兩種特殊情況都很好求,主要是相交的時候,舉正方形為例,

具體計算看程式碼。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const int mod=1e9+7;
const double eps=1e-8;
const long double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll qpow(ll a,ll b){ll t=1;while(b){if(b%2){t=(t*a)%mod;b--;}a=(a*a)%mod;b/=2;}return t;}
int main()
{
    std::ios::sync_with_stdio(false);
    long double n,l,r;
    cin>>n>>l>>r;
    long double c=cos(PI/n)*l,ans;
    if(r>=l)
    {
        ans=PI*r*r;
    }
    else if(r<=c)
    {
        ans=n*l*l*sin(2*PI/n)/2;
    }
    else
    {
        long double d=sqrt(r*r-c*c);
        ans=n*(l*l*sin(2*PI/n)/2-d*c+PI*r*r*(acos(c/r)/PI));
    }
    printf("%.2Lf\n",ans);
    return 0;
}