fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <string>
  4.  
  5. constexpr int Add(int a, int b) { return a + b; }
  6. constexpr int Sub(int a, int b) { return a - b; }
  7. constexpr int Mul(int a, int b) { return a * b; }
  8. constexpr int Div(int a, int b) { return a / b; }
  9.  
  10. template <char Name, int Func(int, int)>
  11. struct Operation
  12. {
  13. constexpr Operation() { }
  14.  
  15. constexpr int operator()(int a, int b) const
  16. {
  17. return Func(a, b);
  18. }
  19.  
  20. std::string ToString() const
  21. {
  22. return std::string(1, Name);
  23. }
  24. };
  25.  
  26. typedef Operation<'+', Add> Plus;
  27. typedef Operation<'-', Sub> Minus;
  28. typedef Operation<'*', Mul> Multiplies;
  29. typedef Operation<'/', Div> Divides;
  30.  
  31. template <int N, class... Args>
  32. struct Check
  33. {
  34. static void Run()
  35. {
  36. Check<N + 1, Plus, Args...>::Run();
  37. Check<N + 1, Minus, Args...>::Run();
  38. Check<N + 1, Multiplies, Args...>::Run();
  39. Check<N + 1, Divides, Args...>::Run();
  40. }
  41. };
  42.  
  43. template <class T1, class T2, class T3, class T4, class T5>
  44. struct Check<5, T1, T2, T3, T4, T5>
  45. {
  46. static const int Result = T5()(T4()(T3()(T2()(T1()(1, 2), 3), 4), 5), 6);
  47.  
  48. static void Run()
  49. {
  50. if (Result == 35)
  51. Print();
  52. }
  53.  
  54. static void Print()
  55. {
  56. std::cout << "(((((1 " << T1().ToString() << " 2) " << T2().ToString() << " 3) " << T3().ToString() << " 4) " << T4().ToString() << " 5) " << T5().ToString() << " 6)" << std::endl;
  57. }
  58. };
  59.  
  60. int main()
  61. {
  62. Check<0>::Run();
  63. return 0;
  64. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
(((((1 + 2) + 3) * 4) + 5) + 6)
(((((1 * 2) * 3) * 4) + 5) + 6)
(((((1 + 2) * 3) * 4) + 5) - 6)