本来题目不难, 这题花了近两个半小时。原因总结:
1由于基础的不扎实, struct 和 operator 认真回顾了一遍, struct 了一个face(面), 命名为f[], 但是不能直接sort的, 所以在struct里又弄了个重载 <, 便于sort, 这样做比重写sort要好。
2对于==的返回值没有及时考虑, 当做=一样来使用, 三个量是否相等, 要分别用三个==和两个&&连接, 不能直接三个==,导致卡了很久。以后不能再犯。
3不要因为舍友的影响, 做好自己
代码如下:
注释写得很认真, 错误也没删, 留着日后警示自己。
//xi3-10 UVa1587Box盒子.cpp
#include
#include
using namespace std;
struct face{ //面
int l, w;
// bool operator < (const NODE& rha) const{
// if(h == rha.h) return w < rha.w;
// return h < rha.h;
bool operator < (const face& abc) const //照着别人搞的,好厉害- -
{ //这样就支持sort了
if(l == abc.l) return w < abc.w; //重载<, f[]间用过l比较
return l < abc.l;
}
}f[10];
//下面的没用了
//fswap(face f[a],face f[b])
//{
// int templ, tempw;
// templ = f[a].l;
// f[a].l = f[b].l;
// f[b].l = templ;
// temp2 = f[a].w;
// f[a].w = f[b].w;
// f[b].w = temp2;
//}
//
//void simpleSort(f[], int n)
//{
// int samll;
// for (int i = 0; i < n-1; ++i)
//}
//bool ok() //a f[i].w) swap(f[i].l, f[i].w); //make l < w
}
// for(int i = 0; i < 6; ++i) printf("f[%d].l = %d\t", i, f[i].l),printf("f[%d].w = %d\n", i, f[i].w);
sort(f, f+6); //f[]从小到大排
// for(int i = 0; i < 6; ++i) printf("f[%d].l = %d\t", i, f[i].l),printf("f[%d].w = %d\n", i, f[i].w);
// printf("POSSIBLE\n");
if(ok()) puts("POSSIBLE");
else puts("IMPOSSIBLE");
}
return 0;
}