CountDiv
Compute number of integers divisible by k in range [a..b].
Task Score
100%
Correctness
100%
Performance
100%
A가 0이면 그냥 B를 K로 나눈 몫에 1을 더해주면 개수가 나온다.
그런데 A가 0이 아니면 B 전에 나오는 K의 배수의 개수에서 A 이전에 나오는 K의 배수들을 빼줘야한다.
원래 (B / K +1) - ((A - 1) / K + 1) 인데 앞 뒤의 1이 서로 상쇄되므로 생략해주었다.
 x
// you can also use imports, for example:// import java.util.*;// you can write to stdout for debugging purposes, e.g.// System.out.println("this is a debug message");class Solution {    public int solution(int A, int B, int K) {                        if (A == 0)            return B / K + 1;        else            return B / K - (A - 1) / K;    }}'공부 > algorithm' 카테고리의 다른 글
| [codility] GenomicRangeQuery (0) | 2018.05.24 | 
|---|---|
| [codility] PassingCars (0) | 2018.05.23 | 
| [codility] MaxCounters (0) | 2018.05.23 | 
| [codility] PermCheck (0) | 2018.05.23 | 
| [codility] MissingInteger (0) | 2018.05.23 | 
댓글