细思极恐怖如斯
观察如下代码:
template<class T> inline void print(T x){
if(x < 0){
putchar('-');
print(-x);
}
else if(x < 10){
putchar(x ^ 48);
}
else{
print(x / 10);
putchar((x % 10) ^ 48);
}
}
十分正常的快速输出?你错了,观察这几行
if(x < 0){
putchar('-');
print(-x);
}
冷知识,int、long long 等类型,拿 int 举例,上限为 \(2^{31} - 1\),下限为 \(-2^{31}\)。
所以我们看似天衣无缝的快写,当我们让它输出 int 类型的 \(-2^{31}\),或是 long long 类型的 \(-2^{63}\) 时,-x 爆了上限,我们的代码会输出 -0。
细思极恐,细思极恐啊。
而且还有很多人这样写。

浙公网安备 33010602011771号