码迷,mamicode.com
首页 > 其他好文 > 详细

nyoj 1099 Lan Xiang's Square (水题)

时间:2015-05-20 09:53:34      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:nyoj1099   nyoj 1099   lan xiangs square   

Lan Xiang‘s Square

时间限制:1000 ms  |  内存限制:65535 KB
难度:0
描述

       Excavator technology which is strong, fast to Shandong to find Lan Xiang.

       Then the question comes.. :)

       for this problem , i will give you four points. you just judge if they can form a square.

       if they can, print "Yes", else print "No".

       Easy ?  just AC it.

输入
T <= 105 cases.
for every case
four points, and every point is a grid point .-10^8 <= all interger <= 10^8。
grid point is both x and y are interger.
输出
Yes or No
样例输入
1
1 1
-1 1
-1 -1
1 -1
样例输出
Yes
提示
you think this is a easy problem ? you dare submit, i promise you get a WA. :)
来源
myself
上传者

ACM_张开创

题意:给出四个点坐标,判断是否能组成正方形。

证明:四条边相等,并且有一个角是直角 为正方形。

题很简单 ,自己的思路也一直整错 可就是wa

可能数据太给力了,调试n次,发现sqrt()导致精度损失。 这个错误可真难找。。。

先贴上wa的代码

 
#include <stdio.h>
#include <math.h>
#define inf 500000005
int main()
{
	double a[4],b[4],x1,x2,x3,x4,y1,y2,y3,y4;
	double sum1,sum2,l1,l2,l3,l4,l;
	int i,t;
	scanf("%d",&t);
	while(t--)
	{
		sum1=sum2=0,x1=x2=x3=x4=y1=y2=y3=y4=inf;
		for(i=0;i<4;i++)
			scanf("%lf %lf",&a[i],&b[i]),sum1+=a[i],sum2+=b[i];
		sum1=sum1/4,sum2=sum2/4;
		for(i=0;i<4;i++)
		{
			if(a[i]<sum1&&b[i]>=sum2)
				x1=a[i],y1=b[i];
			if(a[i]<=sum1&&b[i]<sum2)
				x2=a[i],y2=b[i];
			if(a[i]>=sum1&&b[i]>sum2)
				x3=a[i],y3=b[i];
			if(a[i]>sum1&&b[i]<=sum2)
				x4=a[i],y4=b[i];
		}
		if(x1==inf||x2==inf||x3==inf||x4==inf||y1==inf||y2==inf||y3==inf||y4==inf)
		{
			printf("No\n");
			continue;
		}
		l1=sqrt(pow(y1-y2,2)+pow(x1-x2,2));
		l2=sqrt(pow(y2-y4,2)+pow(x2-x4,2));
		l3=sqrt(pow(y3-y4,2)+pow(x3-x4,2));
		l4=sqrt(pow(y1-y3,2)+pow(x1-x3,2));
		l=sqrt(pow(y3-y2,2)+pow(x3-x2,2));
		if(l1==l2&&l1==l3&&l1==l4&&fabs(l1*l1+l4*l4-l*l)<0.00001)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}                

ac代码

#include <stdio.h>
#include <math.h>
#define inf 500000005
int main()
{
	double a[4],b[4],x1,x2,x3,x4,y1,y2,y3,y4;
	double sum1,sum2,l1,l2,l3,l4,l;//l1,l2,l3,l4,l分别表示四条边和一个对角线。
	int i,t;
	scanf("%d",&t);
	while(t--)
	{
		sum1=sum2=0,x1=x2=x3=x4=y1=y2=y3=y4=inf;//初始坐标
		for(i=0;i<4;i++)
			scanf("%lf %lf",&a[i],&b[i]),sum1+=a[i],sum2+=b[i];
		sum1=sum1/4,sum2=sum2/4;
		for(i=0;i<4;i++)//更新坐标
		{
			if(a[i]<sum1&&b[i]>=sum2)
				x1=a[i],y1=b[i];
			if(a[i]<=sum1&&b[i]<sum2)
				x2=a[i],y2=b[i];
			if(a[i]>=sum1&&b[i]>sum2)
				x3=a[i],y3=b[i];
			if(a[i]>sum1&&b[i]<=sum2)
				x4=a[i],y4=b[i];
		}
		if(x1==inf||x2==inf||x3==inf||x4==inf||y1==inf||y2==inf||y3==inf||y4==inf)//如果有一个坐标未更新 ,no
		{
			printf("No\n");
			continue;
		}
		l1=pow(y1-y2,2)+pow(x1-x2,2);//和wa不同的地方
		l2=sqrt(pow(y2-y4,2)+pow(x2-x4,2));
		l3=sqrt(pow(y3-y4,2)+pow(x3-x4,2));
		l4=pow(y1-y3,2)+pow(x1-x3,2);//同上
		l=pow(y3-y2,2)+pow(x3-x2,2);//同上
		if(sqrt(l1)==l2&&sqrt(l1)==l3&&sqrt(l1)==sqrt(l4)&&l1+l4==l)
			printf("Yes\n");
		else 
			printf("No\n");
	}
	return 0;
}        


nyoj 1099 Lan Xiang's Square (水题)

标签:nyoj1099   nyoj 1099   lan xiangs square   

原文地址:http://blog.csdn.net/su20145104009/article/details/45851015

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!