Last active
June 3, 2016 06:07
-
-
Save lykkin/0f07aaf1ff79be94e317ca5015dda128 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
template <int x, int y> | |
struct gcd { | |
enum { value = gcd<y, x%y>::value }; | |
}; | |
template <int x> | |
struct gcd<x, 0> { | |
enum { value = x }; | |
}; | |
template <int n> | |
struct fibb { | |
enum { value = fibb<n - 1>::value + fibb<n - 2>::value }; | |
}; | |
template <> | |
struct fibb<0> { | |
enum { value = 1 }; | |
}; | |
template <> | |
struct fibb<1> { | |
enum { value = 1 }; | |
}; | |
template <int x, int y, int... rest> | |
struct maxi { | |
enum { value = x > y ? maxi<x, rest...>::value : maxi<y, rest...>::value }; | |
}; | |
template <int x, int y> | |
struct maxi<x, y> { | |
enum { value = x > y ? x : y }; | |
}; | |
int main(int, char *[]) | |
{ | |
cout << "GCD: 5,4: " << gcd<5,4>::value << endl; | |
cout << "GCD: 10, 5: " << gcd<10, 5>::value << endl; | |
cout << "GCD: 18, 8: " << gcd<18, 8>::value << endl; | |
cout << "GCD: 894744, 2312: " << gcd<894744, 2312>::value << endl; | |
cout << "GCD: 100, 10000: " << gcd<100, 10000>::value << endl; | |
cout << "GCD: 40, 50: " << gcd<40,50>::value << endl; | |
cout << "fibb 0: " << fibb<0>::value << endl; | |
cout << "fibb 1: " << fibb<1>::value << endl; | |
cout << "fibb 2: " << fibb<2>::value << endl; | |
cout << "fibb 3: " << fibb<3>::value << endl; | |
cout << "fibb 4: " << fibb<4>::value << endl; | |
cout << "fibb 5: " << fibb<5>::value << endl; | |
cout << "fibb 6: " << fibb<6>::value << endl; | |
cout << "max 5, 6, 7, 8, 9: " << maxi<5, 6, 7,8,9>::value << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment