1 条题解
-
0
【例14.3】 数学课上
解题思路
非负数加 0.5 后向下取整,实现通常四舍五入到整数。
正确性说明
小数部分不足 0.5 时加后不跨整数,达到 0.5 时会进到下一整数。
易错点
题目保证非负;用 floor 而不是依赖银行家舍入。
复杂度
本题输入规模固定,时间 O(1),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); double n; cin >> n; cout << static_cast<long long>(floor(n + 0.5)) << "\n"; return 0; }
- 1