題面
Time limit per test: 2 seconds
Memory limit per test: 256 megabytes
Description
You are given a regular polygon with 2⋅n vertices (it's convex and has equal sides and equal angles) and all its sides have length 1. Let's name it as 2n-gon.
Your task is to find the square of the minimum size such that you can embed 2n-gon in the square. Embedding 2n-gon in the square means that you need to place 2n-gon in the square in such way that each point which lies inside or on a border of 2n-gon should also lie inside or on a border of the square.
You can rotate 2n-gon and/or the square.
Input
The first line contains a single integer T (1≤T≤200) — the number of test cases.
Next T lines contain descriptions of test cases — one per line. Each line contains single even integer n (2≤n≤200). Don't forget you need to embed 2n-gon, not an n-gon.
Output
Print T real numbers — one per test case. For each test case, print the minimum length of a side of the square 2n-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed 10−6.
Example
input
3
2
4
200
output
1.000000000
2.414213562
127.321336469
題意
給定一個邊長為 1 的正 2n 邊形
求外接正方形的最小面積
解題思路 1
因為本題給定的 n 是偶數
故正方形與正 2n 邊形恰好可以有4條邊重合
正八邊形如下:
正十二邊形如下:
以正十二邊形為例,根據對稱性,可以先只看右上的部分:
根據正十二邊形的每條邊再畫出水平和豎直的兩條邊,組成一個直角三角形
可以得到,最上邊截得半條邊,長度為 1/2
在所有斜邊中,因為正多邊形角度平分,所以可以得到每條斜邊與水平邊的夾角是平分90°角的
所以這張圖中的水平長度為
1/2 + 1*cos30° + 1*cos60°
實際正方形邊長為 2*(1/2 + 1*cos30° + 1*cos60°)
推廣出去,對於正十六邊形
在四分之一圖中共有三條斜邊
所以將 90° 角分為 4 份,每份 22.5°
實際邊長為 2*(1/2 + 1*cos22.5° + 1*cos45° + 1*cos67.5°)
可以發現,從四邊形開始,每增加四條邊,斜邊數量+1
故斜邊數量可以通過 n/2-1 得到,每次增加 90/(n/2) °
程序 1
#include<bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
const double epsilon=PI/180.0; //角度轉弧度
void solve()
{
int n;
cin>>n;
double tri=90.0/(n/2),trid=0.0,ans=0;
for(int i=1;i<n/2;i++)
{
trid+=tri;
ans+=cos(trid*epsilon);
}
cout<<fixed<<setprecision(9)<<(ans+0.5)*2<<'\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;cin>>T;
while(T--)
solve();
return 0;
}
解題思路 2
由於本題給定的 n 是偶數
故正方形與正 2n 邊形恰好可以有4條邊重合
所以這個正 2n 邊形的內接圓直徑恰好為外接最小正方形的直徑
以正十二邊形為例
將正 2n 邊形分成 2n 份,得到一個底邊為 1 的等腰三角形
頂角 θ = 360/(2*n) = 180/n
計算可得 內接圓半徑為 0.5/tan(θ/2)
所以答案即 1/tan(θ/2)
程序 2
#include<bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
const double epsilon=PI/180.0; //角度轉弧度
void solve()
{
int n;
cin>>n;
cout<<fixed<<setprecision(9)<<1.0/tan(90.0/n*epsilon)<<'\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;cin>>T;
while(T--)
solve();
return 0;
}