The error in your logic shows up when N
is a multiple of M
, for example the input
6 3 1
produces 4
, but it should produce 3
.
The problem is with this condition:
if (M <= N)
it should be:
if (M < N)
By testing for equality too, the bank robber makes one more trip back to the bank, but there's nothing to collect, so the loop ends (with her at the bank, rather than at the car - another logic problem).
If I were doing this I would not use a loop - I would use purely numerical methods (an exercise for the reader, but hover over the following to see my solution):
int time = (N / M) * 2 * T + (N % M == 0 ? -T : T);