LightOJ 1058 Parallelogram Counting【平行四边形的判定】

    xiaoxiao2025-11-02  5

    题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1058

    题解:判断是否是平行四边形,直接判断这两条对角线的中点在不在一个坐标上就可以了。然后一次记录下所有在一个坐标的中点,再进行组合。

    这个题主要就是这两块,想到了就可以做出来了,想不到就没得玩了。

    #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; #define eps 1e-6 struct point { double x,y; }a[1000+10],b[1000*1000]; bool cmp(point x,point y) { if(x.x==y.x) return x.y>y.y; return x.x>y.x; } double ABS(double x) { if(x<0) return -x; return x; } int main() { int T,kcase=1; int i,j,n; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0;i<n;i++) scanf("%lf%lf",&a[i].x,&a[i].y); int k=0; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { b[k].x=(a[i].x+a[j].x)/2; b[k++].y=(a[i].y+a[j].y)/2; } } sort(b,b+k,cmp); int ans=1,sum=0; for(i=1;i<k;i++) { if(ABS(b[i].x-b[i-1].x)<eps&&ABS(b[i].y-b[i-1].y)<eps) ans++; else { sum+=ans*(ans-1)/2; ans=1; } } sum+=(ans-1)*ans/2; printf("Case %d: %d\n",kcase++,sum); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1303765.html
    最新回复(0)