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.现在你的任务是计算出 ...
随机推荐
- 2nd 燃尽图
燃尽图(burn down chart) 在项目完成之前,对需要完成的工作所作的一种可视化表示.燃尽图主要用于向项目组成员和用户提供一个工作进展的公共视图,用以描述项目的实现状态.一般来说,常常用于形 ...
- egret 开发总结
用egret快两年了,开发过两款成功的游戏.<<妖怪修走 |诸神的黄昏>><<损友圈|我的地盘>> 妖怪修走是个重度游戏,付费率超高.也比较成功. 损友 ...
- Java 中 Vector、ArrayList、List 使用深入剖析
线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...
- ConcurrentHashMap原理分析(1.7与1.8)-put和 get 需要执行两次Hash
ConcurrentHashMap 与HashMap和Hashtable 最大的不同在于:put和 get 两次Hash到达指定的HashEntry,第一次hash到达Segment,第二次到达Seg ...
- Day 2 while循环 编码 and or not
1.判断下列逻辑语句的True,False. 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 Flas ...
- [十七]SpringBoot 之 使用自定义的properties
Springboot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 如果继续在application.properties中添加 如: ...
- BZOJ5072 小A的树(树形dp)
容易猜到能选择的黑点个数是一个连续区间.那么设f[i][j]为i子树内选j个点形成包含根的连通块,最多有几个黑点,g[i][j]为最少有几个黑点,暴力dp是O(n2)的,求出每个连通块大小对应的黑点数 ...
- [BJWC2018]Border 的四种求法
description luogu 给一个小写字母字符串\(S\),\(q\)次询问每次给出\(l,r\),求\(s[l..r]\)的\(Border\). solution 我们考虑转化题面:给定\ ...
- 关于kali linux系统的简单工具
Linux系统中关于几个重要目录的原英文解释: /etc/: Contains configuration files of the installed tools /opt/: Contains M ...
- [POI2011]SEJ-Strongbox
题目大意: 一个有密码箱,数字是0~n-1,其中有若干个密码,密码的特点:若x是密码,y是密码,(x可以等于y)则(x+y)%n也是密码. 给一个n(<=10^14),一个k(k<=min ...