hdu 5533 正n边形判断 精度处理

Dancing Stars on Me

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1098    Accepted Submission(s): 598

Problem Description

The sky was brushed clean by the wind and the stars were cold in a black sky. What a wonderful night. You observed that, sometimes the stars can form a regular polygon in the sky if we connect them properly. You want to record these moments by your smart camera. Of course, you cannot stay awake all night for capturing. So you decide to write a program running on the smart camera to check whether the stars can form a regular polygon and capture these moments automatically.

Formally, a regular polygon is a convex polygon whose angles are all equal and all its sides have the same length. The area of a regular polygon must be nonzero. We say the stars can form a regular polygon if they are exactly the vertices of some regular polygon. To simplify the problem, we project the sky to a two-dimensional plane here, and you just need to check whether the stars can form a regular polygon in this plane.

Input

The first line contains a integer T indicating the total number of test cases. Each test case begins with an integer n, denoting the number of stars in the sky. Following n lines, each contains 2 integers xi,yi, describe the coordinates of n stars.

1≤T≤300
3≤n≤100
−10000≤xi,yi≤10000
All coordinates are distinct.

Output

For each test case, please output "`YES`" if the stars can form a regular polygon. Otherwise, output "`NO`" (both without quotes).

Sample Input

3
3
0 0
1 1
1 0
4
0 0
0 1
1 0
1 1
5
0 0
0 1
0 2
2 2
2 0

Sample Output

NO
YES
NO

Source

2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)

Recommend

hujie   |   We have carefully selected several similar problems for you:  5830 5829 5828 5827 5826

题意:给你n个点(-1e4<x,y<=1e4),判断这n个点能否组成一个正n边形;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
using namespace std;

struct Point{
    int x,y;
    void read()
    {
        scanf("%d%d",&x,&y);
    }
}p[105],tubao[105];

int dcmp(double a)
{
    if(fabs(a)<eps) return 0;
    else if(a>0) return 1;
    else return -1;
}

Point operator-(Point a,Point b)
{
    return (Point){a.x-b.x,a.y-b.y};
}

double dis(Point a)
{
    return sqrt(a.x*a.x+a.y*a.y);
}

double cross(Point a,Point b)
{
    return  a.x*b.y-b.x*a.y;
}

double dot(Point a,Point b)
{
    return a.x*b.x+a.y*b.y;
}

bool cmp(Point a,Point b)
{
    if(a.x!=b.x) return a.x<b.x;
    else return a.y<b.y;
}

int  convex_hull(Point *p,int n,Point *tubao)
{
    sort(p+1,p+n+1,cmp);
    int m=0;
    for(int i=1;i<=n;i++)
    {
        while(m>=2&&cross(p[i]-tubao[m-1],tubao[m]-tubao[m-1])>0) m--;
        tubao[++m]=p[i];
    }
    int k=m;
    for(int i=n-1;i>=1;i--)
    {
        while(m-k>=1&&cross(p[i]-tubao[m-1],tubao[m]-tubao[m-1])>0) m--;
        tubao[++m]=p[i];
    }
    m--;
    return m;
}

int main()
{
    int cas,n;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) p[i].read();
        int k=convex_hull(p,n,tubao);
        tubao[k+1]=tubao[1];

        bool flag=true;
        double tmp=(n-2.0)*pi/n;

        for(int i=1;i<=k-1;i++)
        {
            Point a=tubao[i+1]-tubao[i],b=tubao[i+2]-tubao[i+1];
            double cosang=dot(a,b)/(dis(a)*dis(b));
            double ang=acos(cosang);
            ang=pi-ang;
            if(dcmp(ang-tmp)!=0) {flag=false;break;}
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

  分析:主要是借助这道题来分下下计算几何的精度问题,

double型数据精度处理的两种方式

1.相除改为ong long相乘,这种是肯定对的,不会错。

2.dcmp函数,这种比较简单,但是有一定的精度条件,如果角度是1/999999-1/1000000,那么相减起来就是1e-6*1/999999为1e-12级别,这样是可以使用dcmp的,比如本道题,因为1-e4<=x<=1e4,那么最小的角度差是1/(2*1e4-1)-1/2*1e4(最小的角是1/2*1e4,第二小的角度是1/(2*1e4-1))为1e-8级别>1e-12级别,所以可以用dcmp(eps<1e-12)

时间: 2024-10-05 23:15:11

hdu 5533 正n边形判断 精度处理的相关文章

HDU 5533/ 2015长春区域 G.Dancing Stars on Me 暴力

Dancing Stars on Me Problem Description The sky was brushed clean by the wind and the stars were cold in a black sky. What a wonderful night. You observed that, sometimes the stars can form a regular polygon in the sky if we connect them properly. Yo

HDU 3864 D_num Miller Rabin 质数判断+Pollard Rho大整数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给出一个数N(1<=N<10^18),如果N只有四个约数,就输出除1外的三个约数. 思路:大数的质因数分解只能用随机算法Miller Rabin和Pollard_rho,在测试多的情况下正确率是由保证的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <c

HDU 4458 Shoot the Airplane(计算几何 判断点是否在n边形内)

Shoot the Airplane Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1037    Accepted Submission(s): 175 Problem Description XXX is playing an interesting game which is based on a 2D plane. In thi

Hdu 4458 Shoot the Airplane(判断点在多边形内)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4458 思路:以飞机为参考系,则飞机相对静止,子弹加上水平方向速度-v.则只需枚举时间,判断该时间时点(子弹)是否在多边形(飞机)内.注意g可以为0,分匀变速和匀速.另外本题精度要求较高,判断点在线段上用坐标差值,避免使用Dot . #include<cstdio> #include<cstring> #include<iostream> #include<algori

Hdu 4594 Difference(奇圈判断+差分约束)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4598 思路:由题意可知两相连点ai符号一定相反,所以若存在奇圈则一定无解.染色,colour[i]==1表示为正,colour[i]==2表示为负.由于(b)条件为充要条件,所以对于图中的点| a[i]-a[j] | >= T,对于非图中点| a[i]-a[j] | < T,即| a[i]-a[j] | <= T-1 .所以图中点,若colour[i]==1,a[i]-a[j] >=

HDU 5533 Dancing Stars on Me 计算几何瞎暴力

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1184    Accepted Submission(s): 651 Problem Description The sky was brushed clean by the wind and the stars were cold in a b

HDU 4279 Number 坑爹的迷之精度

题目描述 首先定义"special number": 如果对于一个数字B,存在一个数字A(0<A<=B),并同时满足 B%A=0 和 gcd(A,B) != 1 ,那么我们就说A是B的"special number". 再定义一个函数f(x)表示x的"special number"的数量.并且如果f(x)%2=1时,我们就称x为"real number". 现在给你两个数字x和y且1<=x<=y<

2015ACM/ICPC亚洲区长春站 G hdu 5533 Dancing Stars on Me

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 186    Accepted Submission(s): 124 Problem Description The sky was brushed clean by the wind and the stars were cold in a bl

HDU 1272 小希的迷宫 (判断是否为树)

题意:其实就是让你判断一个图是否为树,要求不能有孤立的点(没有这中情况),且只能有1个连通图,且边数+1=点数,且每个点都有边(不可能只有1个点出现). 思路:有可能出现连续的4个0,也就是有测试例子是完全没有点的,也没有边,要打印Yes!记录下所有点(去重),记录下每个点的度数,如果有n个点,n-1条边,且每点都是有边连着的,再判断其只有一个连通图就行了.因为只有n-1条边,所以当只有一个连通图时,必不会有环的产生. 判断是否只有一个连通图的方法: (1)BFS (2)DFS (3)并查集 (