- 题目:
求能包含 边长为1的正2n边形 的最小正方形,输出正方形的边长。 - 推导思路:
为了一般化推导,这里设正多边形的边长为a。- 当n为偶数时:
可以发现正方形的边长=对边之间的距离: t a n ( θ ) = t a n ( π − π n 2 ) = a n s 2 a 2 \displaystyle tan(\theta)=tan(\frac{\pi-\frac{\pi}{n}}{2})=\frac{\frac{ans}{2}}{\frac{a}{2}} tan(θ)=tan(2π−nπ)=2a2ans a n s = a ∗ t a n ( π − π n 2 ) = a ∗ t a n ( n π − π 2 n ) ans=\displaystyle a*tan(\frac{\pi-\frac{\pi}{n}}{2})=a*tan(\frac{n\pi-\pi}{2n}) ans=a∗tan(2π−nπ)=a∗tan(2nnπ−π) - 当n为奇数时:
最终的正方形肯定不和正多边形的边重合,因为图中的两条绿线不等长,这样得到的不是正方形。
可以画出最终正方形应该是这样的:
对于正2n边形, a 2 = r 2 + r 2 − 2 r r c o s ( π n ) \displaystyle a^2=r^2+r^2-2rrcos(\frac{\pi}{n}) a2=r2+r2−2rrcos(nπ) r 2 = a 2 2 ( 1 − c o s ( π n ) ) \displaystyle r^2=\frac{a^2}{2(1-cos(\frac{\pi}{n}))} r2=2(1−cos(nπ))a2 θ 1 = π n ∗ n 2 \displaystyle \theta_1=\frac{\pi}{n}*\frac{n}{2} θ1=nπ∗2n θ 2 = π n ∗ n + 1 2 \displaystyle \theta_2=\frac{\pi}{n}*\frac{n+1}{2} θ2=nπ∗2n+1 L 1 2 = r 2 + r 2 − 2 r 2 c o s ( θ 1 ) = 2 r 2 ( 1 − c o s ( θ 1 ) ) \displaystyle L_1^2=r^2+r^2-2r^2cos(\theta_1)=2r^2(1-cos(\theta_1)) L12=r2+r2−2r2cos(θ1)=2r2(1−cos(θ1)) L 2 2 = r 2 + r 2 − 2 r 2 c o s ( θ 2 ) = 2 r 2 ( 1 − c o s ( θ 2 ) ) \displaystyle L_2^2=r^2+r^2-2r^2cos(\theta_2)=2r^2(1-cos(\theta_2)) L22=r2+r2−2r2cos(θ2)=2r2(1−cos(θ2)) a n s = L 1 2 + L 2 2 = 2 r 2 ( 1 − c o s ( θ 1 ) ) + 2 r 2 ( 1 − c o s ( θ 2 ) ) 2 \displaystyle ans=\frac{L_1}{\sqrt{2}}+\frac{L_2}{\sqrt{2}}=\frac{\sqrt{2r^2(1-cos(\theta_1))}+\sqrt{2r^2(1-cos(\theta_2))}}{\sqrt{2}} ans=2L1+2L2=22r2(1−cos(θ1))+2r2(1−cos(θ2)) 所以最终: a n s = r 2 ( 1 − c o s ( θ 1 ) ) + r 2 ( 1 − c o s ( θ 2 ) ) \displaystyle ans=\sqrt{r^2(1-cos(\theta_1))}+\sqrt{r^2(1-cos(\theta_2))} ans=r2(1−cos(θ1))+r2(1−cos(θ2))
其中 r 2 = a 2 2 ( 1 − c o s ( π n ) ) \displaystyle r^2=\frac{a^2}{2(1-cos(\frac{\pi}{n}))} r2=2(1−cos(nπ))a2 θ 1 = π n ∗ n 2 \displaystyle \theta_1=\frac{\pi}{n}*\frac{n}{2} θ1=nπ∗2n θ 2 = π n ∗ n + 1 2 \displaystyle \theta_2=\frac{\pi}{n}*\frac{n+1}{2} θ2=nπ∗2n+1
- 当n为偶数时:
- ac代码:
C1
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
const double pi = acos(-1.0);
int t, n;
int main()
{
cin >> t;
while(t--)
{
cin >> n;
printf("%.8f\n", tan((n*pi-pi)/2/n));
}
return 0;
}
C2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
const double pi = acos(-1.0);
int t, n;
int main()
{
cin >> t;
while(t--)
{
cin >> n;
double r2 = 1.0/(2-2*cos(pi/n));
double ang1 = pi/n*(n/2), ang2 = pi/n*((n+1)/2);
double ans = sqrt(r2*(1-cos(ang1)))+sqrt(r2*(1-cos(ang2)));
printf("%.8f\n", ans);
}
return 0;
}