我前面提到的语句在做什么?
#include <bits/stdc++.h>
using namespace std;
int64_t dist(int64_t sx, int64_t sy, int64_t ex, int64_t ey) {
sx += ey - sy;
int64_t ret = (ey - sy)*2;
if (sx%2 != ex%2)
ret += (sx+ey)%2 ? 3 : 1, sx++; // i don't understand this line,Please explain it.
ret += (ex - sx)*2;
return ret;发布于 2019-10-26 19:05:20
这是一个三元条件运算符,允许您在为变量赋值时避免多语句if-else构造。它的使用形式是
variable = condition ? value_if_true : value_if_false。
+=运算符将值添加到某个变量,并且等于ret = (ret + (sx+ey)%2 ? 3 : 1, sx++);
%运算符用于计算两个数字的余数。
++用于将1添加到给定的变量中,在本例中,它在将1添加到ret之后立即执行(如果该行上的条件为false)。
无论如何,正如注释中所指出的,这段代码真的是不可读的,如果我们将它拆分为几个语句,不会发生什么不好的事情。
if (sx % 2 != ex % 2) {
if ((cx + ey)) % 2) {
ret += 3;
} else {
ret++;
}
sx++;
}https://stackoverflow.com/questions/58570156
复制相似问题