题解 | #取近似值#
取近似值
https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
这样的题目无聊,高版本C++不建议float +0.5 然后转换int。这里我这么写竟然通过了所有的test case。
不知道CLion上报的警告,是那种case过不了
#include <iostream>
class HJ07 {
public:
int round(float x) {
return int(x+0.5);
}
};
int main(int argc, char *argv[]) {
float x = 0.0;
HJ07 q;
std::cin >> x;
std::cout << q.round(x) << "\n";
return 0;
}
Clang-Tidy: Casting (double + 0.5) to integer leads to incorrect rounding; consider using lround (#include <cmath>) instead
