Skip to content

Instantly share code, notes, and snippets.

@Starl1ght
Created February 13, 2017 06:23
Show Gist options
  • Save Starl1ght/667374a176cd3c3e75e3b09302d010e7 to your computer and use it in GitHub Desktop.
Save Starl1ght/667374a176cd3c3e75e3b09302d010e7 to your computer and use it in GitHub Desktop.
FizzBuzz
#include <cstdio>
#include <cstdlib>
template <int P, bool d3, bool d5>
struct print {
static void Do();
};
template <int P>
struct print<P, false, false> {
static void Do() { printf("%i\n", P); }
};
template <int P>
struct print<P, true, false> {
static void Do() { printf("%i %s\n", P, "Fizz"); }
};
template <int P>
struct print<P, false, true> {
static void Do() { printf("%i %s\n", P, "Buzz"); }
};
template <int P>
struct print<P, true , true> {
static void Do() { printf("%i %s\n", P, "FizzBuzz"); }
};
template <int A, int B, int R>
struct iterate {
static void Do() {
print<A, A % 3 == 0, A % 5 == 0>::Do();
iterate<A + R, B, R>::Do();
}
};
template <int A, int R>
struct iterate<A, A, R> {
static void Do() {
print<A, A % 3 == 0, A % 5 == 0>::Do();
}
};
template <int A, int B>
void FizzBuzz() {
iterate<A, B, (A > B ? -1 : 1)>::Do();
}
void main() {
FizzBuzz<20, 0>();
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment