Guest User

Untitled

a guest
Oct 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. namespace prob21 {
  5.  
  6. typedef double(*Func)(double,double);
  7.  
  8. double add(double a, double b){return a+b;}
  9. double sub(double a, double b){return a-b;}
  10. double mul(double a, double b){return a*b;}
  11. double div(double a, double b){return a/b;}
  12.  
  13. bool eq(double a, double b)
  14. {
  15. return std::abs(a-b) < 0.01;
  16. }
  17.  
  18. const Func funcs[4] = {add, sub, mul, div};
  19. const char funcNames[4] = {'+', '-', '*', '/'};
  20. const int vals[6] = {1,2,3,4,5,6};
  21. const double target = 35;
  22.  
  23. char path[5];
  24.  
  25. void print(double acc)
  26. {
  27. for (int i=0; i<5; ++i)
  28. std::cout << '(';
  29. std::cout << vals[0];
  30. for (int i=1; i<6; ++i)
  31. std::cout << path[i-1] << vals[i] << ')';
  32. std::cout << '=' << acc << std::endl;
  33. }
  34.  
  35. void dig(int ind, double acc)
  36. {
  37. if (ind==5) {
  38. if (eq(acc, target))
  39. print(acc);
  40. return;
  41. }
  42.  
  43. for (int i=0; i<4; ++i) {
  44. path[ind] = funcNames[i];
  45. Func f = funcs[i];
  46. double v = vals[ind+1];
  47. dig(ind+1, f(acc, v));
  48. }
  49. }
  50.  
  51. void solve()
  52. {
  53. dig(0, vals[0]);
  54. }
  55.  
  56. }
  57.  
  58. int main()
  59. {
  60. prob21::solve();
  61.  
  62. return 0;
  63. }
Add Comment
Please, Sign In to add comment