1587

来源:互联网 发布:pe系统制作软件 编辑:程序博客网 时间:2024/05/18 16:37

紫书上习题3-10;,我做这道题的思路是:给你6个矩阵的长和宽,先在结构体中定义长和宽,然后让这6个结构体全排列(长度由小到大,长度相同的话,让宽由小到大排列),排列完后,判断相邻的两个结构体是否一致,如果不一致就输出 IMPOSSINBLE ,如果全部一致,再判断0,2,4或者1,3,5这三个结构体中的数据,这时0,1,2,3中的长肯定一致,判断0中的宽和5中的长以及3中的宽和5中的宽是不是一样,若一样,则输出 POSSIBLE ,否则输出  IMPOSSIBLE ;

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <iomanip>#include <algorithm>using namespace std;struct pallet{  int x,y;};int is_samestruct(struct pallet a,struct pallet b){    if (a.x==b.x&&a.y==b.y)        return 1;    else        return 0;}int main(){    struct pallet bb[6];    while(cin>>bb[0].x>>bb[0].y>>bb[1].x>>bb[1].y>>bb[2].x>>bb[2].y>>bb[3].x>>bb[3].y>>bb[4].x>>bb[4].y>>bb[5].x>>bb[5].y)    {        for(int i=0;i<6;i++) {           if(bb[i].x>bb[i].y)              swap(bb[i].x,bb[i].y);        }        for(int i=0;i<6;i++){            for(int j=i+1;j<6;j++){                if(bb[i].x>bb[j].x){ swap(bb[i],bb[j]); }            }        }        for(int i=0;i<6;i++){            for(int j=i+1;j<6;j++){                if(bb[i].x==bb[j].x&&bb[i].y>bb[j].y)                    swap(bb[i],bb[j]);            }        }        int flag=1;        for (int i=0;i<6;i+=2)            if (!is_samestruct(bb[i],bb[i+1])){               flag=0;cout<<"IMPOSSIBLE"<<endl; break;            }        if(flag==1){            if(bb[0].x==bb[2].x){                if(bb[0].y==bb[4].x&&bb[2].y==bb[4].y)                    cout<<"POSSIBLE"<<endl;                else                    cout<<"IMPOSSIBLE"<<endl;            }            else                cout<<"IMPOSSIBLE"<<endl;        }    }    return 0;}


1 0
原创粉丝点击