NYOJ 141 Squares (数学)
2024-08-31 01:22:07
描述
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.
- 输入
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.You can assume the number of test cases is less than 20 - 输出
For each test case, print on a line the number of squares one can form from the given stars. - 样例输入
4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0 - 样例输出
1
6
1
分析:
题目的意思其实很简单,就是对于给出的一系列二维坐标系中的点,其中的四个点构成一个正方形,这样不同的正方形一共有多少个。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
struct Node
{
int x;
int y;
} node[1009];
bool cmp(Node a,Node b)///这些点如果横坐标相同的话,就按照纵坐标从小到大排序;否则就直接按照横坐标从小到大排序
{
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
bool Search(int x,int y)///采用二分查找法,查一下求出的点是否在已有点的序列中
{
int left=0;
int right=n;
int mid;
while(left<=right)
{
mid=(left+right)/2;
if(node[mid].x==x&&node[mid].y==y)
return true;
else if(node[mid].x==x&&node[mid].y<y||node[mid].x<x)///递归在右区间中找
left=mid+1;
else///递归在左区间中找
right=mid-1;
}
return false;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
int ans=0;
for(int i=0; i<n; i++)
scanf("%d%d",&node[i].x,&node[i].y);
sort(node,node+n,cmp);
int x1,y1,x2,y2;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
///这样找的话相当于找的是这条线上方或左方的图形,不理解的话自己用坐标试试
x1=node[j].x-(node[j].y-node[i].y);
y1=node[j].y+(node[j].x-node[i].x);
if(!Search(x1,y1)) continue;
x2=node[i].x-(node[j].y-node[i].y);
y2=node[i].y+(node[j].x-node[i].x);
if(!Search(x2,y2)) continue;
ans++;
}
printf("%d\n",ans/2);///每一个图形都可以由两条边找到,下面和右面的边
}
return 0;
}
最新文章
- Javascirpt中创建对象的几种方式
- Android可移动控件
- Python进程、线程
- (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug
- flexpaper源码的编译,去除logo和打印 (转)
- 大型Web应用运行时 PHP负载均衡指南
- [React] React Router: activeStyle &; activeClassName
- QQ联系客服
- poj 3761 Bubble Sort_快速幂
- hdu 4778 Rabbit Kingdom(减少国家)
- mysqldump 命令的使用
- 关于oracle数据库(4)数据类型
- 邓_laravel框架——news
- python的IndentationError: unexpected indent python
- Spring boot + mybatis + orcale实战(干货)
- python学习第4天
- codevs 1795 金字塔 2
- *2_3_5_加入reference model
- 与http协作的web服务器、http首部(第五章、第六章)
- Apache的Order Allow,Deny详解
热门文章
- Python——cmd调用(os.system阻塞处理)(多条命令执行)
- 小程序解密 encryptedData 获取 unionID 等信息
- Android中res/layout文件夹里新建布局文件,R中不生成ID的奇葩错误
- 一次性无重复配置VS项目插件属性的方法
- C# WebBrowser控件模拟登录
- 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。
- App简介及登录页面
- 洛谷U14200 Changing 题解 【杨辉三角】
- redis的自带VM(虚拟内存)
- Linux内核分析第三周学习博客——跟踪分析Linux内核的启动过程