题解 | #取近似值#

取近似值

https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a

利用int取出整数部分,作差取出小数部分。与0.5作比较。对整数部分四舍五入

#include <iostream>
using namespace std;

int main() {
    float input = 0;
    cin >> input;

    int x = int(input);
    float y = input - x;
    if(y >= 0.5)
        cout << x+1 << endl;
    else
        cout << x << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

今天 11:53
门头沟学院 Java
投递百度等公司7个岗位
点赞 评论 收藏
分享
06-15 18:44
黄淮学院 Java
Lynn012:如果是居民楼还是算了吧,看着有点野呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务