关于merge函数 提供一种错误写法
查看原帖
关于merge函数 提供一种错误写法
585332
ewe_楼主2022/11/21 21:47
// 错误写法
node merge(node a, node b) {
	node res;
	res.cx = a.cx + b.cx;
	res.lx = a.lx + a.cy ? 0 : b.lx;
	res.rx = b.rx + b.cy ? 0 : a.rx;
	res.mx = max(max(a.mx, b.mx), a.rx + b.lx);
	res.cy = a.cy + b.cy;
	res.ly = a.ly + a.cx ? 0 : b.ly;
	res.ry = b.ry + b.cx ? 0 : a.ry;
	res.my = max(max(a.my, b.my), a.ry + b.ly);
// rx ry lx ly 的写法是有误的
// 三目运算符的优先级比加法低?
	return res;
}

// 正确写法如下
node Merge(node a, node b) {
	node res;
	res.cx = a.cx + b.cx;
	res.lx = a.cy ? a.lx : a.cx + b.lx;
	res.rx = b.cy ? b.rx : b.cx + a.rx;
	res.mx = max(max(a.mx, b.mx), a.rx + b.lx);
	res.cy = a.cy + b.cy;
	res.ly = a.cx ? a.ly : a.cy + b.ly;
	res.ry = b.cx ? b.ry : b.cy + a.ry;
	res.my = max(max(a.my, b.my), a.ry + b.ly);
	return res;
}

2022/11/21 21:47
加载中...