poj 2002 Squares
来源:互联网 发布:js date 获取当前日期 编辑:程序博客网 时间:2024/04/27 21:59
对于给定的点,可以组成多少个正方形。这个题目还是比较简单的了。首先建给点的点进行排序。按照横坐标从小到大的顺序来排序,如果横坐标相同,就按照纵坐标从小到大的顺序来排序。我们可以找到任意的两个点。通过公式,计算出以这两个点为边的另外的两个点。这样的四个点可以组成一个正方形。
公式如下:
a.x=p[i].x+p[j].y-p[i].y; a.y=p[i].y+p[i].x-p[j].x; b.x=p[j].x+p[j].y-p[i].y; b.y=p[j].y+p[i].x-p[j].x;这个公式,通过验证,可以知道的得到的ab边跟原来的p[i]p[j]边的位置关系。通过这种方式来枚举正方形的另外两个顶点。查找这两个顶点是否在刚才的排序的点集中。如果能够找到。就让计数器加1。由于这样枚举,可以使得一个多边形算两次,所以得到的结果应该除以2才是最终的结果。
还有要说的是,用到了STL中的binary_search()函数,感觉相当的好用。但是,前提要保证查找的元素所在的序列是有序的。有四个参数,查找序列的首地址,序列的末地址+1,待查找的元素,查找函数cmp;第四个参数需要说一下。一般是你的数组按照何种方式排序,就要按照何种方式查找,所以STL中sort的cmp如何写,binary_serch()中的cmp就应该如何写,也就是说,这两个函数可以共用一个cmp;
#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>#include<string.h>#include<queue>#include<climits>#include<list>#include<vector>using namespace std;#define N 1002struct point{ int x,y;}p[N];bool cmp(point a,point b){ if(a.x==b.x)return a.y<b.y; return a.x<b.x;}int main(){ int n; while(scanf("%d",&n)&&n) { for(int i=0;i<n;i++) { scanf("%d%d",&p[i].x,&p[i].y); } sort(p,p+n,cmp); int cn=0; point a,b; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { a.x=p[i].x+p[j].y-p[i].y; a.y=p[i].y+p[i].x-p[j].x; b.x=p[j].x+p[j].y-p[i].y; b.y=p[j].y+p[i].x-p[j].x; if(!binary_search(p,p+n,a,cmp))continue; if(!binary_search(p,p+n,b,cmp))continue; cn++; } } printf("%d\n",cn/2); } return 0;}
- poj 2002 Squares
- poj 2002 Squares
- POJ 2002 Squares
- poj 2002 Squares
- poj 2002 Squares
- poj-2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares
- BIT1033 POJ 2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares Hash
- POJ 2002 Squares
- poj 2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares
- poj-2002-Squares--二分
- poj 2002 Squares
- [观点]成为编程巨星的唯一秘诀
- java 2-4
- 我的一天我做主(Enjoy)- 3
- php学习笔记 php中面向对象三大特性之一[封装性]的应用
- java 2-5
- poj 2002 Squares
- 控制数据库两个小数相除后的小数位数
- Loadrunner Controller cannot create Vusers.的解决办法
- java 交通灯
- 搜索引擎迈进第一步
- tomcat6w.exe 运行 提示 指定的服务未安装 unable to open the service 'tomcat6'
- 初学UML
- hdu1907博弈
- Linux各发行版本优缺点分析