알고리즘/codefights

19>rangeBitCount

Diademata 2021. 4. 17. 17:09
반응형

For a = 2 and b = 7, the output should be
rangeBitCount(a, b) = 11.

Given a = 2 and b = 7 the array is: [2, 3, 4, 5, 6, 7]. Converting the numbers to binary, we get [10, 11, 100, 101, 110, 111], which contains 1 + 2 + 1 + 2 + 2 + 3 = 11 1s.

Input/Output

[execution time limit] 0.5 seconds (cpp)

[input] integer a

Guaranteed constraints:
0 ≤ a ≤ b.

[input] integer b

Guaranteed constraints:
a ≤ b ≤ 10.

 

code>>

 

int rangeBitCount(int aint b) {

    auto count = 0;

    for (int i = ai <= b; ++i)

    {

        std::bitset<4bit(i);

        count += bit.count();

    }

    return count;

}

 

반응형

'알고리즘 > codefights' 카테고리의 다른 글

18>arrayPacking  (0) 2021.04.17
17>Kill K-th Bit  (0) 2021.04.17
Challenge>formatString  (0) 2019.03.06
Challenge>fibonacciIndex  (0) 2019.03.06
16>metroCard  (0) 2019.02.24