鏈接: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;
}
}
}