题解 | #取近似值#
取近似值
https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
#include "bits/stdc++.h" using namespace std; int main() { string a; while (cin >> a) { int i = 0, dec=0; for (; i < a.size(); ++i) { if (a[i] == '.') { dec = i; break; } } int num = a[i+1] - '0'; if (num >= 5) { i -= 1; while (i>= 0 && a[i] + 1 > '9') { i-=1; } if (a[i] + 1 > '9') { a = "1" + a.substr(0, dec+1); cout << a.substr(0, dec+1) << endl; } else { a[i] += 1; cout << a.substr(0, dec) << endl; } } else { cout << a.substr(0, dec) << endl; } } }
四舍五入:
转化为字符串操作,可以处理任意长度的浮点数。
#美团求职进展汇总##国央企薪资爆料#