ZOJ-2347

又是判断正方形,这题竟然写了我两个晚上。。大部分时间全是在纸上画几何坐标系求已知两点的另外两点组成正方形的点。。我的高中数学知识啊。。太悲伤了,这种简单的几何题竟然想了这么久。。想当年切平面几何切菜一样的。。唉,还好最后还是把公式推出来了,剩下的代码就简单了

#include<stdio.h>
#include<stdlib.h>

struct Point
{
	int x;
	int y;
};

static int cmp_point(const void *p1, const void *p2)
{
	struct Point *pp1 = (struct Point*) p1;
	struct Point *pp2 = (struct Point*) p2;
	if (pp1->x != pp2->x)
		return pp1->x - pp2->x;
	else
		return pp1->y - pp2->y;
}

int main()
{
	int N, n;
	scanf("%d", &N);
	struct Point *points = malloc(1000 * sizeof(struct Point));
	struct Point *p1 = malloc(sizeof(struct Point));
	struct Point *p2 = malloc(sizeof(struct Point));
	while (N--)
	{
		while (scanf("%d", &n), n)
		{
			int i, j;
			for (i = 0; i < n; i++)
				scanf("%d %d", &(points[i].x), &(points[i].y));
			qsort(points, n, sizeof(struct Point), cmp_point);

			int x1, x2, y1, y2, squares = 0;
			for (i = 0; i < n; i++)
				for (j = i + 1; j < n; j++)
				{
					x1 = points[i].x;
					y1 = points[i].y;
					x2 = points[j].x;
					y2 = points[j].y;

					p1->x = y2 - y1 + x1;
					p1->y = x1 - x2 + y1;
					p2->x = y2 - y1 + x2;
					p2->y = y2 + x1 - x2;
					if (bsearch(p1, points, n, sizeof(struct Point),
							cmp_point) != NULL&&bsearch(p2, points, n, sizeof(struct Point),
									cmp_point) != NULL)
						squares++;
					p1->x = y1 - y2 + x1;
					p1->y = x2 - x1 + y1;
					p2->x = x2 + y1 - y2;
					p2->y = x2 - x1 + y2;
					if (bsearch(p1, points, n, sizeof(struct Point),
							cmp_point) != NULL&&bsearch(p2, points, n, sizeof(struct Point),
									cmp_point) != NULL)
						squares++;

				}
			printf("%d\n", squares / 4);
		}
		if (N)
			putchar('\n');
	}
	free(p1);
	free(p2);
	free(points);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值