酷代码 AI
菜单
服务商

小数四舍五入与取整结果相等个数统计

💻以下是实现该功能的 C++ 代码: ```cpp #include <iostream> #include <cmath> using namespace std; int main() { int n; cin >> n; double num; int upCount = 0, downCount = 0; for (int i = 0; i < n; i++) { cin >> num; int rounded = round(num); int up = ceil(num); int down = floor(num); if (rounded == up) { upCount++; } if (rounded == down) { downCount++; } } cout << upCount << " " << downCount; return 0; } ``` ### 代码解释 1. **输入读取**:先读取数字个数 `n`,接着通过循环读取每个小数。 2. **计算结果**:对每个小数进行四舍五入、向上取整和向下取整操作。 3. **统计个数**:若四舍五入结果与向上取整相等,`upCount` 加 1;若与向下取整相等,`downCount` 加 1。 4. **输出结果**:输出 `upCount` 和 `downCount`。 ######[AI问答 | 312点数解答 | 2026-05-16 20:04:10]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]