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.现在你的任务是计算出 ...
随机推荐
- java.lang.NoClassDefFoundError: Lcom/opensymphony/xwork2/util/logging/Logger tomcat6 启动错误
用tomcat6启动时,出现下面的错误Java.lang.NoClassDefFoundError: Lcom/opensymphony/xwork2/util/logging/Logger; Cau ...
- VSVC2010中常用的C++11特性
static_assert 声明 static_assert 声明在编译时测试软件断言,这与在运行时进行测试的其他断言机制不同. 如果断言失败,则编译也将失败,且系统将发出指定的错误消息. const ...
- 控件属性和InitializeComponent()关系:
namespace Test22 { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </summary> priv ...
- JMeter脚本增强之参数化
JMeter测试脚本录制或者编写,在Web应用和App上的操作方式可能有一点点区别(其实也差不多,哈哈),但是当脚本录制好了之后,对测试脚本的强化,包括参数化.关联.文本检查.集合点设置,甚至再往后的 ...
- .net MVC中使用angularJs刷新页面数据列表
使用angularjs的双向绑定功能,定时刷新页面上数据列表(不是刷新网页,通过ajax请求只刷新数据列表部分页面),实例如下: @{ Layout = null; } <!DOCTYPE ht ...
- SQL入门之查询入门
select语法一般结构: SELECT [ALL|DISTINCT] <目标列表达式> [别名] [, <目标列表达式> [别名]]... FROM <表名或视图名&g ...
- Spring Boot 初步小结
Spring Boot 是一种开发模式,不涉及任何新的技术 1.了解自动配置的原理 2.常用application.yml文件的配置项 3.Spring Boot 及 第三方提供的各种 starter ...
- UVA11736_Debugging RAM
题目绝对够水,我就不详细说明了. 直接上代码吧.只是提示一下要用 unsigned long long. (不知道我不用字典树为什么会超时,肿么搞的) #include <iostream> ...
- innerHTML与innerText的区别: 前者获取的是dom对象内的所有html元素 后者获取的是dom对象里面的纯文本元素
- 【纪念】NOIP2018前夕——一些想说的话
刚刚复习了一下相关的内容,决定一会儿就洗洗睡了.在睡觉之前,决定写点东西. 有的时候真的很迷茫,选择了一条超过自己能力范围的路,每天挣扎在各种各样难题的面前,文化成绩一落千丈……在从前觉得这一切都是有 ...