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.现在你的任务是计算出 ...
随机推荐
- python基础(一)简单入门
一.第一个python程序 1.交互式编程 直接在命令行里面输入python即可进入python交互式命令行,linux下一样: 在 python 提示符中输入以下文本信息,然后按 Enter 键查看 ...
- Filter2D卷积运算
图像处理中的卷积运算一般都用来平滑图像.尖锐图像求边缘等等.主要看你选择什么样的核函数了.现在核函数很多,比如高斯平滑核函数,sobel核函数,canny核函数等等.这里举一个sobel核函数的例子来 ...
- PHP对象的遍历
对象的遍历 对象的遍历,跟数组的遍历,一样! 其实,只能遍历出对象的“实例属性数据” foreach( $对象名 as $key => $value){ //这里就可以处理$key和$va ...
- H5跳转到百度地图并定位
找了半天的JS api,发现没有,后来发现这个叫 url api,让我好找. 官方文档: http://lbsyun.baidu.com/index.php?title=uri/api/web : 简 ...
- 【JavaScript】JAVA-表格里的c:foreach使用及数字总计
两步:1.上图 2.上代码 <div class="group-accordion" collapsible="true" active="tr ...
- P2774 方格取数问题
题目背景 none! 题目描述 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.对于 ...
- mysql索引长度的一些限制
一.myisam存储引擎 1. 数据库版本:阿里云RDS MySQL5.1 mysql> select @@version;+-------------------------------+| ...
- Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)
链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS Memory Limit: 131072K To ...
- 基于数组实现Java 自定义Stack栈类及应用
栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出( Last-in-first-out,LIFO)的原则.java本身是有自带Stack类包,为了达到学习目的已经更好深入了解sta ...
- 《Java程序设计》第8周学习总结 20165218 2017-2018-1
20165218 2017-2018-1 <Java程序设计>第8周学习总结 教材学习内容总结 第12章 java多线程机制 java中的线程 计算机在任何给定时刻只能执行一个线程,多线程 ...