输入六个面,12个数字,判断其能不能构成一个长方体:
问题思路:
首先调整一下每个面的长和宽,然后在按照长边,短边排序一下,然后判断其是否有三对面,两两相同的面。
#include <iostream>
#include< cstring>
using namespace std;
struct face
{
int x, y;
}a[6];
bool check()
{
if(memcmp(a, a+1, sizeof(face)) || memcmp(a+2, a+3, sizeof(face)) || memcmp(a+4, a+5, sizeof(face)))
return false;
if(a[0].x!=a[2].x || a[0].y!= a[4].x || a[2].y!=a[4].y) return false;
return true;
}
int main()
{
while(cin >> a[0].x >> a[0].y >> a[1].x >> a[1].y >> a[2].x >> a[2].y >> a[3].x >> a[3].y >> a[4].x >> a[4].y >> a[5].x >> a[5].y){
for(int i = 0; i < 6; ++i)
if(a[i].x < a[i].y)
swap(a[i].x, a[i].y); //把单个面的长调整一下
sort(a, a+6, [](const face a, const face b) {return a.x==b.x ? (a.y > b.y) : (a.x > b.x);});
//排序
printf("%s\n", check() ? "POSSIBLE" : "IMPOSSIBLE");
}
return 0;
}