题解 | #取近似值#

取近似值

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

#include <iostream>
using namespace std;
/* 1. 用字符串 string 获得输入值
    2. 找到 find函数 字符串中小数点的位置
    3. 判断小数点后的 子字符串 的首字符 是否大于等于 5
    4. 大于等于5 则向上取整,否则,不向上取整
*/
int main() {
    string str;
    getline(cin, str);
    size_t res = 0;

    size_t pos = str.find('.');
    str = str.substr(0, pos);
    if(str[pos+1] >= '5'){
        res = stoi(str) + 1;
    }else{
        res = stoi(str);
    }

    cout << res << endl;
    return 0; 
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务