Compile-Time Timer Reload

#include <iostream>
using namespace std;

// Compute timer reload at compile time (with rounding)
constexpr unsigned int computeReload(unsigned int clockHz,
                                     unsigned int intervalMs) {
    return (clockHz * intervalMs + 500) / 1000;
}

// Precomputed reload values for 1 ms, 10 ms, 100 ms
constexpr unsigned int RELOAD_TABLE[3] = {
    computeReload(1'000'000, 1),
    computeReload(1'000'000, 10),
    computeReload(1'000'000, 100)
};

int main() {
    int idx;
    cin >> idx;

    cout << RELOAD_TABLE[idx];
    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

1000