A - Water Station Editorial by en_translator
If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
We introduce two solutions: one focused on calculations, and another on searching. There are other correct solutions too.
Solution 1
This solution is focused on calculations.
Dividing everything by \(5\), we need to find the nearest whole of \(x=\dfrac N5\). This can be found by rounding the fractional part of \(x\) to the nearest integer.
Thus, the answer is \(N\) divided by \(5\), rounded to the nearest whole, then multiplied by \(5\). If you write a code like this, your program will be accepted.
N = int(input())
print(round(N / 5) * 5)
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N;
cin >> N;
cout << int(round(N / 5.0)) * 5 << endl;
return 0;
}
Conversion from \(\dfrac N5\) to a floating point number involves an error, but under the printed result will be exact under the constraints of this problem.
Alternatively, we can find the answer only within integers (without errors). We do not prove the correctness of the expression here (Hint: rounding to the nearest whole = add \(\dfrac12\), then round down).
N = int(input())
print(((N + 2) // 5) * 5)
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
cout << ((N + 2) / 5) * 5 << endl;
return 0;
}
Solution 2
We now move on to the searching-oriented solution.
The candidates of the answer are the following \(21\) integers: \(0,5,10,\ldots\), and \(100\). Check all of them, and print the one that is nearest to \(W\), to solve this problem.
N = int(input())
ans = 100
for i in range(0, 101, 5):
if abs(N - ans) > abs(N - i):
ans = i
print(ans)
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N;
cin >> N;
int ans = 100;
for (int i = 0; i <= 100; i += 5) {
if (abs(N - ans) > abs(N - i)) {
ans = i;
}
}
cout << ans << endl;
return 0;
}
posted:
last update: