nyoj 1099 Lan Xiangs Square (水题)

题目1099题目信息运行结果本题排行讨论区

Lan Xiang’s Square

时间限制:1000ms | 内存限制:65535KB

难度:0

描述

Excavatortechnologywhichis strong,fastto Shandong to findLan 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 casefour 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样例输入11 1-1 1-1 -11 -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 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]<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");elseprintf("No\n");}return 0;}ac代码

#include <stdio.h>#include <math.h>#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]<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");elseprintf("No\n");}return 0;}

,经验是由痛苦中粹取出来的

nyoj 1099 Lan Xiangs Square (水题)

相关文章:

你感兴趣的文章:

标签云: