UVa-1587-Box
心得:0. 注意printf和endl 的缓冲区别!
1.注意swap和sort的细节,调试了半天,就是因为不注意细节,比如下面这个,低级错误。。。。不能再犯!
void stand() {
if (x > y)
{
int temp = x;
x = y;
y = x;
}
}
2.注意C++的封装特性,比如定义了< 、> 、== 、 sort这些函数,把实现封装之后,易于调试!
虽然别人都说是水题。。。果然还是我太菜了,虽然样例输出正确,但总是WA,弄得很烦躁
昨天"蛙"(wa)声一片,今天重新做了一遍,终于AC了,写的类很粗糙,定义了自己的sort函数和swap函数。
先将六个面排序,然后根据六面体的特性,用flag作为标志位,控制输入输出。
#include<iostream>
#include<string>
using namespace std;
struct face {
int x;
int y;
void stand() {
if (x > y)
{
int temp = x;
x = y;
y = temp;
}
}
};
int theswap(face&a, face& b);
bool operator== (face& a, face& b);
bool operator< (face& a, face& b);
bool operator> (face& a, face& b);
int thesort(face* box);
face box[6];
int main(void)
{
while (cin >> box[0].x >> box[0].y >> box[1].x >> box[1].y
>> box[2].x >> box[2].y >> box[3].x >> box[3].y
>> box[4].x >> box[4].y >> box[5].x >> box[5].y)
{
for (int i = 0; i < 6; i++) {
box[i].stand();
}
thesort(box);
int flag = 0;
if (box[0] == box[1] && box[2] == box[3] && box[4] == box[5]
&& box[0].x == box[2].x && box[0].y == box[4].x && box[2].y == box[4].y)
flag = 1;
if (flag) printf("POSSIBLE\n");
else printf("IMPOSSIBLE\n");
flag = 0;
}
}
bool operator== (face& a, face& b)
{
return (a.x == b.x && a.y == b.y);
}
bool operator< (face& a, face& b)
{
if (a.x > b.x) return false;
else if (a.x < b.x) return true;
else if (a.y > b.y) return false;
else if(a.y < b.y) return true;
else false;
}
bool operator> (face& a, face& b)
{
return !(a < b) && !(a == b);
}
int theswap(face&a, face& b)
{
int temp = a.x;
a.x = b.x;
b.x = temp;
temp = a.y;
a.y = b.y;
b.y = temp;
return 0;
}
int thesort(face* box)
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 5 - i; j++)
{
if (box[j] > box[j + 1]) theswap(box[j], box[j + 1]);
}
}
return 0;
}