题解 | #取近似值#
取近似值
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")