uva1587是否得到长方体

预备知识

std::swap(T&a,T&b)

{
  T c(a); a=b; b=c;
}

memcmp()  //#include <string.h>

1. int memcmp(const void *str1, const void *str2, size_t n));其功能是把存储区 str1 和存储区 str2 的前 n 个字节进行比较。该函数是以字典序按字节比较的。str1>str2返回1,str1=str2返回0,str1<str2返回-1.

2. char *s1="abc";  char *s2="acd";  int r=memcmp(s1,s2,3); 就是比较s1和s2的前3个字节,第一个字节相等,第二个字节比较中大小已经确定,不必继续比较第三字节了。所以r=-1.

3. 自定义结构第三个参数用sizeof(T)

sort()  //#include <algorithm>

1.sort(start,end,compare())第二个参数是结束地址的下一个地址,第三个参数没有时默认升序。bool compare(T a,T b){....return ..;}返回值控制如何排序

2.1sort(start,end)默认升序:

1  int a[7] = { 2,1,5,7,3,4,3 };
2     sort(a,a+7);
3     for (int i = 0;i < 7;++i)
4   cout << a[i] << " "; //1,2,3,3,4,5,7 

sort(start,end,greater<int>())为降序排序;

2.2自定义降序:

bool compare(int a,int b)
{ return a > b; }; int a[7] = { 3,5,1,7,4,2,8 }; sort(a,a+7,compare);  

 2.3自定义类型的字典序升序:

struct two
{
    int x;int y;
}arr[7];
bool compare(two a,two b)
{
    if (a.x != b.x)return a.x < b.x;
    else return a.y < b.y;
};
 sort(arr,arr+7,compare);//arr[i]按照xy字典序升序

    ;

2.4自定义升序降序混合:

bool compare(two a,two b){

if (a.x != b.x)return a.x < b.x;
else return a.y > b.y;
};

 

2.5当compare(T a,T b)中return a==b时,不排序。

算法思路

1.连续输入六*2条边存入a[6],元素类型为有两条边x,y的自定义结构。确保x>=y.将六个面按照字典序降序排序.对于长方体,长宽高a>=b>=c时六个面降序排序后变为ab,ab,ac,ac,bc,bc.

2.比较每两个面是否相等

3.判断a[0].x=a[2].x;a[0].y=a[4].x;a[2].y=a[4].y

代码

#include <bits/stdc++.h>
using namespace std;
struct face{
    int x, y;
}a[6];
bool check()
{  //判断三对面是否相同,都成对时memcmp都返回0,则继续下一条语句
    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);});
//compare()函数可以直接以[](){}的形式作为第三个参数 printf("%s\n", check() ? "POSSIBLE" : "IMPOSSIBLE"); } return 0; }

  

  

 

 

 

 

 

 

 

 

 

 

 

posted @ 2020-10-31 01:39  __小王子  阅读(66)  评论(0)    收藏  举报