[codility]CountDiv

https://codility.com/demo/take-sample-test/count_div

此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到。

int solution(int A, int B, int K) {

    // write your code in C++11

    int diff = (B-A+1);

    int result = diff / K;

    for (int i = A + result * K; i <= B; i++)

    {

        if (i % K == 0)

            result++;

    }

    return result;

}

  

你可能感兴趣的:(count)