题意:给你空间中四个点,问你这四个点能否组成正方形。
解题思路:看两两之间的距离。根据正方形的性质求解。
解题代码:
1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月07日 星期二 00时41分28秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 #define eps 1e-8 26 using namespace std; 27 struct node{ 28 double x,y,z; 29 }; 30 node a[5]; 31 double dis[5][5]; 32 double thedis(double x1, double y1,double z1, double x2,double y2,double z2) 33 { 34 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2) *(z1-z2)) ; 35 } 36 bool ok() 37 { 38 for(int i = 1;i <= 4;i ++) 39 { 40 for(int j= 1;j <= 4 ; j ++) 41 { 42 dis[i][j] = thedis(a[i].x,a[i].y,a[i].z,a[j].x,a[j].y,a[j].z); 43 } 44 sort(dis[i] + 1,dis[i] + 1 + 4); 45 } 46 for(int i = 2;i <= 4;i ++) 47 for(int j = 1;j <= 4; j ++) 48 { 49 if(fabs(dis[i][j] - dis[i-1][j]) > eps) 50 return 0 ; 51 } 52 if(fabs(dis[1][4]-0 ) <eps) 53 return 0 ; 54 if(fabs(dis[1][3] - dis[1][2]) > eps) 55 return 0 ; 56 if(fabs(dis[1][3] * sqrt(2) - dis[1][4]) > eps) 57 return 0 ; 58 if(fabs(dis[1][3] - 0 ) < eps) 59 return 0 ; 60 if(fabs(dis[1][2] - 0 ) < eps) 61 return 0 ; 62 return 1; 63 } 64 int main(){ 65 int n; 66 //freopen("A.in","r",stdin); 67 //freopen("A.out","w",stdout); 68 scanf("%d",&n); 69 for(int ca = 1 ; ca <= n ; ca ++) 70 { 71 for(int i= 1;i <= 4;i ++) 72 scanf("%lf %lf %lf",&a[i].x,&a[i].y,&a[i].z); 73 printf("Case #%d: ",ca); 74 if(ok()) 75 { 76 printf("Yes\n"); 77 }else { 78 printf("No\n"); 79 } 80 } 81 return 0; 82 }
时间: 2024-12-15 00:13:32