HDOJ 2056
1 #include<iostream> 2 using namespace std; 3 4 #include<algorithm> 5 #include<iomanip> 6 #include<cmath> 7 8 int main() 9 { 10 double x[4],y[4]; 11 while(cin>>x[0]>>y[0]) 12 { 13 for(int i = 1;i < 4;i++) 14 { 15 cin>>x[i]>>y[i]; 16 } 17 if(max(x[0],x[1]) <= min(x[2],x[3]) || max(x[2],x[3]) <= min(x[0],x[1]) || 18 max(y[0],y[1]) <= min(y[2],y[3]) || max(y[2],y[3]) <= min(y[0],y[1])) 19 cout<<fixed<<setprecision(2)<<0.00<<endl; 20 else 21 { 22 sort(x,x+4); 23 sort(y,y+4); 24 cout<<fixed<<setprecision(2)<<abs(x[2]-x[1])*abs(y[2]-y[1])<<endl; 25 } 26 27 } 28 29 return 0; 30 }
第二种方法:
1 #include<iostream> 2 using namespace std; 3 4 #include<cmath> 5 #include<iomanip> 6 7 8 typedef struct point 9 { 10 double x,y; 11 }point; 12 13 bool isIn(point p1,point p2,point p3) 14 { 15 int a,b,c,d; 16 a = p1.x > p2.x?p2.x:p1.x; 17 b = p1.x < p2.x?p2.x:p1.x; 18 c = p1.y > p2.y?p2.y:p1.y; 19 d = p1.y < p2.y?p2.y:p1.y; 20 if(p3.x <= b && p3.x >= a && p3.y <= d && p3.y >= c) return true; 21 else return false; 22 } 23 24 double area(point p1,point p2) 25 { 26 double x = fabs(p1.x-p2.x); 27 double y = fabs(p1.y-p2.y); 28 return x*y; 29 } 30 31 void swift(point &p1,point &p2) 32 { 33 int ta; 34 if(p1.x > p2.x && p1.y < p2.y || p1.x < p2.x && p1.y > p2.y) 35 { 36 ta = p1.y; 37 p1.y = p2.y; 38 p2.y = ta; 39 } 40 } 41 42 void swift2(point &p1,point &p2) 43 { 44 int ta; 45 if(p1.x > p2.x && p1.y > p2.y || p1.x < p2.x && p1.y < p2.y) 46 { 47 ta = p1.y; 48 p1.y = p2.y; 49 p2.y = ta; 50 } 51 } 52 53 int main() 54 { 55 point p1,p2,p3,p4; 56 while(cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y) 57 { 58 point np1,np2; 59 swift(p1,p2); 60 swift(p3,p4); 61 np1.x = np1.y = np2.x = np2.y = -1; 62 if(isIn(p1,p2,p3) && isIn(p1,p2,p4)) 63 { 64 np2 = p4; 65 np1 = p3; 66 cout<<fixed<<setprecision(2)<<area(np1,np2)<<endl; 67 continue; 68 } 69 else if(isIn(p3,p4,p1) && isIn(p3,p4,p2)) 70 { 71 np2 = p2; 72 np1 = p1; 73 cout<<fixed<<setprecision(2)<<area(np1,np2)<<endl; 74 continue; 75 } 76 if(isIn(p1,p2,p3)) 77 np2 = p3; 78 else if(isIn(p1,p2,p4)) 79 np2 = p4; 80 if(isIn(p3,p4,p1)) 81 np1 = p1; 82 else if(isIn(p3,p4,p2)) 83 np1 = p2; 84 if(np1.x == -1 && np2.x == -1 && np1.y == -1 && np2.y == -1) 85 { 86 swift2(p1,p2); 87 swift2(p3,p4); 88 if(isIn(p1,p2,p3) && isIn(p1,p2,p4)) 89 { 90 np2 = p4; 91 np1 = p3; 92 cout<<fixed<<setprecision(2)<<area(np1,np2)<<endl; 93 continue; 94 } 95 else if(isIn(p3,p4,p1) && isIn(p3,p4,p2)) 96 { 97 np2 = p2; 98 np1 = p1; 99 cout<<fixed<<setprecision(2)<<area(np1,np2)<<endl; 100 continue; 101 } 102 if(isIn(p1,p2,p3)) 103 np2 = p3; 104 else if(isIn(p1,p2,p4)) 105 np2 = p4; 106 if(isIn(p3,p4,p1)) 107 np1 = p1; 108 else if(isIn(p3,p4,p2)) 109 np1 = p2; 110 } 111 else 112 { 113 if(np1.x == -1 && np1.y == -1) 114 { 115 if(np2.x = p3.x) 116 { 117 np1.y = p4.y; 118 if(p1.x >= p3.x && p1.x <= p4.x || p1.x <= p3.x && p1.x >= p4.x) 119 np1.x = p1.x; 120 else 121 np1.x = p2.x; 122 } 123 else 124 { 125 np1.y = p3.y; 126 if(p1.x >= p3.x && p1.x <= p4.x || p1.x <= p3.x && p1.x >= p4.x) 127 np1.x = p1.x; 128 else 129 np1.x = p2.x; 130 } 131 } 132 else if(np2.x == -1 && np2.y == -1) 133 { 134 if(np1.x = p1.x) 135 { 136 np2.y = p2.y; 137 if(p3.x >= p1.x && p3.x <= p2.x || p3.x <= p1.x && p3.x >= p2.x) 138 np2.x = p3.x; 139 else 140 np2.x = p4.x; 141 } 142 else 143 { 144 np2.y = p1.y; 145 if(p3.x >= p1.x && p3.x <= p2.x || p3.x <= p1.x && p3.x >= p2.x) 146 np2.x = p3.x; 147 else 148 np2.x = p4.x; 149 } 150 } 151 } 152 153 154 cout<<fixed<<setprecision(2)<<area(np1,np2)<<endl; 155 } 156 157 return 0; 158 }