1 条题解
-
0
练10.2 浮点数向零舍入
解题思路
将浮点数转换为整数,C++ 转换规则会直接舍去小数部分。
正确性说明
舍去小数对正数相当于向下,对负数相当于向上,统一为向零舍入。
易错点
不能使用 floor,因为负数 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 x; cin >> x; cout << static_cast<long long>(x) << "\n"; return 0; }
- 1