A - Takoyaki Editorial by evima
First, find how many times he has to use the machine, then multiply by the time required to use it, and you can find the answer.
The number of times to use the machine can be calculated as \(N / X\), rounded up. You may want to use ceiling function ceil
and write like ceil(N/X)
, but if you use it you will get wrong answer in most languages.
There are multiple ways to handle with it, but writing as follows is most easy to understand.
int times;
if(N % X == 0) times = N / X;
else times = N / X + 1;
In most language, integer divisions are rounded down, so the rounded-up quotient can be calculated by adding \(+1\) only when the remainder is rounded down.
In fact, this calculation can be also done by the following expression.
int times = (N + X - 1) / X;
In this equation, \(X - 1\) is added to the dividend so that the answer increases by \(1\) unless it is exactly divisible. This technique is frequently used, so it may be useful to remember.
If you want to use ceil function, you can write as
int times = (int)ceil((double)N / X);
but it is not recommended because you have to be careful to precision error, as it temporarily converts integer operations to real-number operations.
Sample Code (C++)
#include <iostream>
using namespace std;
int main(){
//recieve inputs
int N, X, T;
cin >> N >> X >> T;
//find the number of times to use the machine
int times = (N + X - 1) / X;
//find the answer
int ans = times * T;
//output the answer
cout << ans << endl;
}
posted:
last update: