因此,我设置了一个代码来查找用户输入与 51 之间差异的大小(绝对值)。如果用户输入大于 51,则结果将增加三倍。不复杂。为了尽量减少代码本身,我想出了这个。
// Compare and determine the correct output based on program's
// paramters:
//
// - find absolute value of input - 51
// - if input > 51 then multiply result by 3
//-----------------------------------------------------------
int calcDiff(int x) {
const int base = 51;
int result = x - base;
return x > base ? 3*result : (result < 0 ? ~result + 1 : result);
}
所以问题是:
这合适吗?我知道它有效,但我更好奇这是否是不好的做法,并且有一天会在后面咬我。scanf
这只是意见,还是像使用or一样大不gets
?有打死马的表情吗?因为我想用它。