nyoj 1099 Lan Xiang's Square (水题)
题目1099题目信息运行结果本题排行讨论区
Lan Xiang's Square
1000 ms | 内存限制:
65535
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. :)
题意:给出四个点坐标,判断是否能组成正方形。
证明:四条边相等,并且有一个角是直角 为正方形。
题很简单 ,自己的思路也一直整错 可就是wa
可能数据太给力了,调试n次,发现sqrt()导致精度损失。 这个错误可真难找。。。
先贴上wa的代码
#include #include #define inf 500000005int 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]=sum2) x1=a[i],y1=b[i]; if(a[i]<=sum1&&b[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 #include #define inf 500000005int 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]=sum2) x1=a[i],y1=b[i]; if(a[i]<=sum1&&b[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;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~