链接:https://www.nowcoder.com/acm/contest/163/D
来源:牛客网
题目描述
In order to become a magical girl, Thinking-Bear are learning magic circle.
He first drew a regular polygon of N sides, and the length of each side is a.
He want to get a regular polygon of N sides, and the polygon area is no more than L.
He doesn't want to draw a new regular polygon as it takes too much effort.
So he think a good idea, connect the midpoint of each edge and get a new regular polygon of N sides.
How many operations does it need to get the polygon he want?
输入描述:
The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve. The first line of each case contains three space-separated integers N, a and L (3 ≤ N ≤ 10, 1 ≤ a ≤ 100, 1 ≤ L ≤ 1000).
输出描述:
For each test case, output a single integer.
示例1
输入
1 4 2 3
输出
1
题解:题意很简单就是不断计算变小的正n边形的面积,看是否小于等于一开始给出的面积,小于时输出此时进行的轮数;
tips:内层边长为:len=sin((n-2)*M_PI/2/n)*a; (a为原正n边形面积)
sin()函数中放入的是弧度需转化,M_PI是预定义好的π;
面积计算可以用公式去推:设正n边形的半径为R,边长为an,中心角为αn,边心距为rn,则αn=360°÷n,an=2Rsin(180°÷n),rn=Rcos(180°÷n),R^2=r n^2+(an÷2)^2,周长pn=n×an,面积Sn=pn×rn÷2。
在储存数据的时候最好用double,其他队用的float可能因为精度问题WA了;
#include<bits/stdc++.h>
using namespace std;
int main()
{
double area,jiao,rn,pn,len,de,a;
int n,t,k=0;
cin>>t;
while(t--)
{
cin>>n>>a>>de;
area=n*a*a/(4*tan(M_PI/n));//一开始的正n边形的面积
len=a;
if(area<=de)
{
cout<<"0"<<endl;
}
else
{
for(int i=1;; i++)
{
len=sin((n-2)*M_PI/2/n)*len;//随着轮数变化的边长
area=n*len*len/(4*tan(M_PI/n));//面积
if(area<=de)
{
k=i;
break;
}
}
cout<<k<<endl;
}
}
}