UVA1587

题意很简单,给六个面判断是否能组成一个长方体。

教训:这类考察思维而不是代码能力的(ACM基本都是这样)题目,要基于题目特性去思考,善于发现物体的特征,再根据特征来写解题方法。

思路:根据长方体特性,将其每个面的数据调整为长在前,宽在后,便于比较,而后根据长和宽来排序,如果两个面长相等就看宽。排序后观察,利用其数据特点来解题。

例:排序后3 2    3 2      3 1      3 1      2 1      2 1。

前四个面长相等,后四个面宽相等。前两个面的宽是最后两个面的长。

代码:

#include<bits/stdc++.h>
using namespace std;
struct box{
    int x,y;
}a[6];
bool cmp(const box a,const box b)
{
    return a.x==b.x?a.y>b.y:a.x>b.x;
}
int main()
{
    while(cin>>a[0].x>>a[0].y)
    {
        int ans=1;
        if(a[0].x<a[0].y)    swap(a[0].x,a[0].y);
        for(int i=1;i<6;i++)
        {
            cin>>a[i].x>>a[i].y;
            if(a[i].x<a[i].y)    swap(a[i].x,a[i].y);
        }
        sort(a,a+6,cmp);
        if(memcmp(a,a+1,sizeof(box))||memcmp(a+2,a+3,sizeof(box))||memcmp(a+4,a+5,sizeof(box)))//memcmp排序,按空间中字符比较,相等返回0. 
            ans=0;
        if(a[0].x!=a[2].x||a[2].y!=a[4].y||a[0].y!=a[4].x)
            ans=0;
        if(ans)    cout<<"POSSIBLE"<<endl;
        else    cout<<"IMPOSSIBLE"<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/depth/p/5669243.html