1 条题解
-
0
练42.3 浮点数向零舍入
解题思路
按题意以float读入,再转换为int。浮点到整数转换会舍去小数,方向是趋向0。
正确性说明
计算规则直接来自题目定义。按所述顺序处理时,已完成部分的结果被准确保存在累加量、计数或当前状态中;下一步只加入新增贡献。最终得到完整要求的结果。
易错点
负数不能用floor;本题输入为单精度。
复杂度
时间O(1),空间O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); float x; cin >> x; cout << static_cast<int>(x) << "\n"; return 0; }
- 1