题意:
给你正n边形上的三个点,问n最少为多少
思路:
三个点在多边形上,所以三个点的外接圆就是这个正多边形的外接圆,余弦定理求出每个角的弧度值,即该角所对边的圆周角,该边对应的圆心角为圆心角的二倍。同时这个圆心角应为正多边形的每条边对应圆心角的整数倍,即2*pi/i的整数倍,遍历for i 1 to 1000 ,判断A*i/pi是否均为整数即可
坑点:
注意判断double是否为整数:return a-(int)(a+eps);
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<stack> #include<queue> #include<deque> #include<set> #include<vector> #include<map> #include<functional> #define fst first #define sc second #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lc root<<1 #define rc root<<1|1 #define lowbit(x) ((x)&(-x)) using namespace std; typedef double db; typedef long double ldb; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> PI; typedef pair<ll,ll> PLL; const db eps = 1e-5; const int mod = 1e9+7; const int maxn = 5e6+2; const int maxm = 2e6+100; const int inf = 0x3f3f3f3f; const db pi = acos(-1.0); struct point{ double x, y; point(double x = 0, double y = 0):x(x), y(y){} }; double length(point a, point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } bool ok(double a){ return a > 0 &&fabs(a-(int)(a+eps)) < eps; } int main(){ point a, b, c; while(scanf("%lf %lf", &a.x, &a.y) != 0){ scanf("%lf %lf %lf %lf", &b.x, &b.y, &c.x, &c.y); double ab = length(a, b); double ac = length(a, c); double bc = length(b, c); double A = acos((ab*ab+ac*ac-bc*bc)/(2.0*ab*ac)); double B = acos((ab*ab+bc*bc-ac*ac)/(2.0*ab*bc)); double C = acos((ac*ac+bc*bc-ab*ab)/(2.0*ac*bc)); int flg = 0; for(int i = 3; i <= 1000; i++){ if(ok(A*i/pi) && ok(B*i/pi) && ok(C*i/pi)){ flg = i; break; } } printf("%d ", flg); } return 0; }