链接: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边形,每条边的边长为a,然后给你一个限定的面积l,每次操作我们取边长的中点,依次连接这些中点,然后会构成一个新的n边形,直到构成的新的n边形的面积小于限定的面积l,结束操作,问你最少需要几次这样的操作。
思路:
思路其实很简单,对于每次操作完成后,我们都求一下新生成的多边形的面积,然后与l对比即可。关键在于如何求多边形的面积。
我们可以通过分割求出多边形的面积,通过上图可知,红线为该多边形内切圆的半径,n个三角形的面积相加就是该多边形的面积,对于这个三角形的边长,高如何求,我们可以通过正弦,余弦,正切来求。
代码:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#define pi 3.1415926535898
typedef long long ll;
using namespace std;
int t;
double n,a,l;
int main()
{
cin>>t;
while (t--)
{
cin>>n>>a>>l;
int count1=0;
double op=(360/n)*(pi/180)/2;//将角度制转化为弧度制,因为cos,sin,tan函数的输入值要求为弧度制
double r,s;//当前n边形的内切圆的半径,n边形的面积
while (1)
{
if(!count1)//刚开始的时候是一个特例,必须单独计算
{
r=(a/2)/tan(op);//求内切圆的半径
s=n*((a*r)/2);
count1++;//其实这里不应该+1,但是为了上面的判断,我们加上,输出的时候减一就行了
if(s<=l)
{
cout<<count1-1<<endl;
break ;
}
}
else
{
double a1=2*(r*sin(op));//这个地方求的是新生成的n边形的边长,通过sin来求,上一个n边形的内切圆半径在这里就用到了
r=(a1/2)/tan(op);//更新内切圆半径
s=n*((a1*r)/2);
count1++;
if(s<=l)
{
cout<<count1-1<<endl;
break ;
}
}
}
}
}
收获:
知道了sin函数,cos函数和tan函数及其用法。(必须传入弧度制,不能够传入角度制)。