题目链接:http://poj.org/problem?id=2160
因为VJ上的这道题加载不出来,幸好POJ上也有
题意:一共6行,每行2个数,代表6个长方形的长和宽。问这6个长方形能不能组成一个长方体。能输出POSSIBLE,不能输出IMPOSSIBLE。
解:先排序,我的做法就是让每个长方形的x<=y,然后排序时先按x排序,x相同按y排序,这样的。
然后这6个长方形一定是a0和a1,a2和a3,a4和a5都是一样的,否则就可以判断不能了。
然后就只剩3个了,假设这三个的序号是b0,b1,b2。
然后假设这个长方体的长宽高是i,j,k。不妨假设i<=j<=k。那么必有b0.x==i,b1.x==i,b0.y==j,b1.y==k,b2.x==j,b2.y==k。
就是说,b0.x==b1.x,b0.y==b2.x,b1.y==b2.y。满足就是能,不满足就是不能。
然后就结束了。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; struct node { int x, y; node(){ x = 0; y = 0; } node(int a, int b) { if (a < b) x = a, y = b; else x = b, y = a; } bool operator !=(node a) { if (this->x == a.x&&this->y == a.y) return false; else return true; } }; bool cmp(node a, node b) { if (a.x < b.x) return true; else if (a.x == b.x&&a.y < b.y) return true; else return false; } bool work() { node a1[6], a2[3]; for (int i = 0; i < 6; ++i) { int a, b; scanf("%d%d", &a, &b); a1[i] = node(a, b); } sort(a1, a1 + 6, cmp); for (int i = 0; i < 3; ++i) { if (a1[2 * i] != a1[2 * i + 1]) return false; a2[i] = a1[2 * i]; } if (a2[0].x == a2[1].x&&a2[0].y == a2[2].x&&a2[1].y == a2[2].y) return true; return false; } int main() { //freopen("input.txt", "r", stdin); if (work()) printf("POSSIBLE\n"); else printf("IMPOSSIBLE\n"); //system("pause"); //while (1); return 0; }