这道题学到最多的就是
给你四个点的坐标,判断这四个点能不能组成一个正方形
AC代码
#include<set>
#include<cmath>
#include<iostream>
using namespace std;
set<double>st;
double x[4],y[4];
double dirt[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
double dist(int a,int b) {
return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}
bool cacu() {
st.clear();
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(i==j) continue;
st.insert(dist(i,j));
}
}
if(st.size()!=2) return false;
return true;
}
int main() {
bool flag=0;
for(int i=0; i<4; i++) {
cin>>x[i]>>y[i];
}
if(cacu()) {
cout<<"wen"<<endl;
} else {
for(int j=0; j<4; j++) {
for(int i=0; i<4; i++) {
x[j]+=dirt[i][0];
y[j]+=dirt[i][1];
if(cacu()) {
flag=1;
break;
}
x[j]-=dirt[i][0];
y[j]-=dirt[i][1];
}
if(flag) break;
}
if(flag) cout<<"hai xing"<<endl;
else cout<<"wo jue de bu xing"<<endl;
}
return 0;
}
只要判断是不是两两边之间的距离只有两种就可以
第一次没能想到只要有两种就可以,还加上了判断
double k=sqrt(2);
double edge1=*st.begin();
st.erase(st.begin());
double edge2=*st.begin();
if(edge1*k==edge2) return true;
结果只过了70%的测试数据,想不太明白,是因为精度的问题吗?