NYOJ 141 Squares (数学)
描述
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;
}
NYOJ 141 Squares (数学)的更多相关文章
- Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calcula ...
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- nyoj 122-Triangular Sums (数学之读懂求和公式的迭代)
122-Triangular Sums 内存限制:64MB 时间限制:3000ms 特判: No 通过数:5 提交数:7 难度:2 题目描述: The nth Triangular number, T ...
- Codeforces 599D Spongebob and Squares(数学)
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculati ...
- NYOJ 330 一个简单的数学
一个简单的数学题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 zyc近期迷上了数学,一天,dj想出了一道数学题来难住他.算出1/n,但zyc一时答不上来希望大家能 ...
- NYOJ 127 星际之门(一) (数学)
题目链接 描述 公元3000年,子虚帝国统领着N个星系,原先它们是靠近光束飞船来进行旅行的,近来,X博士发明了星际之门,它利用虫洞技术,一条虫洞可以连通任意的两个星系,使人们不必再待待便可立刻到达目的 ...
- CF 599D Spongebob and Squares(数学)
题目链接:http://codeforces.com/problemset/problem/599/D 题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个.2x ...
- nyoj 7 街区最短路径问题 【数学】
找出横纵坐标的中位数,怎么找:先对x排序找x的中位数x0,再对y排序找y的中位数y0:最后统计各点到中位数点(x0, y0)的总距离: 街区最短路径问题 时间限制:3000 ms | 内存限制:6 ...
- NYOJ 69 数的长度(数学)
数的长度 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 N!阶乘是一个非常大的数,大家都知道计算公式是N!=N*(N-1)······*2*1.现在你的任务是计算出 ...
随机推荐
- 3dContactPointAnnotationTool开发日志(二七)
今天的主要工作是把选中物体以及复制删除物体和右边三个面板联系起来,就是通过鼠标框选住物体,右边面板的对应项的颜色也会改变,而且通过右边面板也能控制物体的选中状态,被选中的物体成cyan青色,并且包 ...
- 用iptables做代理
出于安全考虑,Linux系统默认是禁止数据包转发的.配置Linux系统的ip转发功能,打开系统转发功能:echo "1" > /proc/sys/net/ipv4/ip_fo ...
- Java多线程编程(学习笔记)
一.说明 周末抽空重新学习了下多线程,为了方便以后查阅,写下学习笔记. 有效利用多线程的关键是理解程序是并发执行而不是串行执行的.例如:程序中有两个子系统需要并发执行,这时候需要利用多线程编程. 通过 ...
- 第157天:canvas基础知识详解
目录 一.canvas简介 1.1 什么是canvas?(了解) 1.2 canvas主要应用的领域(了解) 二.canvas绘图基础 2.0 sublime配置canvas插件(推荐) 2.1 Ca ...
- stm32中使用#pragma pack(非常有用的字节对齐用法说明)
#pragma pack(4) //按4字节对齐,但实际上由于结构体中单个成员的最大占用字节数为2字节,因此实际还是按2字节对齐 typedef struct { char buf[3];//bu ...
- Xor Sum HDU - 4825(01字典序板题)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
- 【刷题】洛谷 P3806【模板】点分治1
题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接 ...
- HBase多租户机制分析
在HBase1.1.0发布之前,HBase同一集群上的用户.表都是平等的,没有优劣之分.这种’大同’社会看起来完美,实际上有很多问题.最棘手的主要有这么两个,其一是某些业务较其他业务重要,需要在资源有 ...
- <深入理解计算机系统>第七章读书笔记
第七章读书笔记 链接 链接:将各种代码和数据部分收集起来并组合成为一个单一文件的过程.(这个文件可被加载或拷贝到存储器并执行) 链接可以执行于编译,加载或运行时. 静态链接: 两个主要任务: 1 符号 ...
- Linux必知必会——xargs命令
1.功能: xargs可以将stdin中以空格或换行符进行分隔的数据,形成以空格分隔的参数(arguments),传递给其他命令.因为以空格作为分隔符,所以有一些文件名或者其他意义的名词内含有空格的时 ...