fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T, size_t Length> struct Sum {
  5. template<class Array>
  6. static T comp(const Array &x, T add = 0) {
  7. return Sum<T, Length - 1>::comp(x, add + Length * x[Length - 1]);
  8. }
  9. };
  10.  
  11. template<class T> struct Sum<T, 0> {
  12. template<class Array>
  13. static T comp(const Array &x, T add = 0) {
  14. return add;
  15. }
  16. };
  17.  
  18. constexpr int d[] = { 0, 1, 1, 0, 1 };
  19. constexpr int e[] = { 1, 0, 3, 2, 4 };
  20.  
  21. template<int N> struct Comp {
  22. template<class Array>
  23. static int comp(const Array &x) {
  24. return d[N] ? Sum<int, e[N]>::comp(x) : 10;
  25. }
  26. };
  27.  
  28. int main() {
  29. int x[] = { 1, 3, 5, 7, 9 };
  30.  
  31. cout << Comp<0>::comp(x) << endl;
  32. cout << Comp<1>::comp(x) << endl;
  33. cout << Comp<2>::comp(x) << endl;
  34. cout << Comp<3>::comp(x) << endl;
  35. cout << Comp<4>::comp(x) << endl;
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
10
0
22
10
50