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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值