Contest Duration: - (local time) (100 minutes) Back to Home

A - Round decimals Editorial by en_translator

There are two routes to solve this problem.

One is to read the input as a string, then convert the substring before . (the decimal point) to the integer, and then increment if the character right after . or the third last character is $$5$$ or more.

Another is, depending on programming language, to use a nearest-whole library equipped to each language. If the behavior for the boundary, that is, when the decimal part is $$0.5$$, is undefined or inappropriate, or not known, then you may avoid it by adding a sufficiently small positive number (like $$0.0005$$ in this case).
In Python, without this tweak the operation against $$0.5$$ will fail, resulting in WA.

Sample code in C++ (with the first route):

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
string X;
int a;
int ans = 0;

cin >> X;
int n = X.size();
rep(i, n - 4) {
a = (int)(X[i] - '0');
ans = 10 * ans + a;
}
if (X[n - 3] >= '5')ans++;
cout << ans << endl;
return 0;
}

Sample code in C++ (with the second route):

#include <bits/stdc++.h>

using namespace std;

int main(void) {
double a;
cin >> a;
cout << (int)round(a+0.0005) << endl;
//cout << (int)round(a) << endl; でも良い
return 0;
}

Sample code in C++ (with the third route):

x=float(input())
print(int(round(x+0.0005,0)))

posted:
last update: