See the figure above.
When A % K == 0, the answer can be given by (B - A) / K + 1.
When A % K != 0, the answer can be given by (B - (A - A % K)) / K.
----------------------------------------
SOLUTION
----------------------------------------
int solution(int A, int B, int K)
{
if (A % K == 0){
return (B - A) / K + 1;
}
return (B - (A - A % K)) / K;
}
0 件のコメント:
コメントを投稿