|
// Compile time Generative Linear Recursion
#include <iostream>
// Josephus Problem
template <int No> struct JosephusProblem
{
enum { value = No % 2 == 0 ? 2 * JosephusProblem<No / 2>::value - 1 :
2 * JosephusProblem<No / 2>::value + 1 };
};
// termination condition
template <> struct JosephusProblem<0>
{
enum { value = 1 };
};
int main()
{
std::cout << JosephusProblem<5>::value << std::endl;
std::cout << JosephusProblem<8>::value << std::endl;
std::cout << JosephusProblem<4>::value << std::endl;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.