daily pastebin goal
63%
SHARE
TWEET

DikiyC

a guest Nov 8th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. / ConsoleApplication1.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. /*
  7. #include <iostream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. int sigma(int n, int m, function<int(int)> pf)
  13. {
  14.     int i, sum;
  15.     sum = 0;
  16.     for (i = n; i <= m; i++)
  17.         sum = sum + pf(i);
  18.     return sum;
  19. }
  20.  
  21. int pi(int n, int m, function<int(int)> pf)
  22. {
  23.     int i, pr;
  24.     pr = 1;
  25.     for (i = n; i <= m; i++)
  26.         pr = pr * pf(i);
  27.     return pr;
  28. }
  29.  
  30. int simple(int i)
  31. {
  32.     return i;
  33. }
  34.  
  35. int fact(int n)
  36. {
  37.     return pi(1, n, simple);
  38. }
  39.  
  40. void main()
  41. {
  42.     int n;
  43.     cin >> n;
  44.     cout << sigma(1, n, fact) << endl;
  45.     system("pause");
  46. }
  47. */
  48.  
  49. /*
  50. #include <iostream>
  51. #include <functional>
  52.  
  53. using namespace std;
  54.  
  55. int summ(int a, int b)
  56. {
  57.     return a + b;
  58. }
  59.  
  60. int mult(int a, int b)
  61. {
  62.     return a * b;
  63. }
  64.  
  65. int simple(int i)
  66. {
  67.     return i;
  68. }
  69.  
  70. int alpha(int n, int m, int start, function<int(int)> pf, function<int(int, int)> pf2)
  71. {
  72.     int i, res;
  73.     res = start;
  74.     for (i = n; i <= m; i++)
  75.         res = pf2(res, pf(i));
  76.     return res;
  77. }
  78.  
  79. int fасt(int n)
  80. {
  81.     return alpha(1, n, 1, simple, mult);
  82. }
  83.  
  84. void main()
  85. {
  86.     int n;
  87.     cin >> n;
  88.     cout << alpha(1, n, 0, fасt, summ) << endl;
  89.     system("pause");
  90. }
  91. */
  92.  
  93. #include <iostream>
  94. #include <functional>
  95.  
  96. using namespace std;
  97.  
  98. int alpha(int m, int n, int start, function<int(int)> pf, function<int(int, int)> pf2)
  99. {
  100.     int i, res;
  101.     res = start;
  102.     for (i = m; i <= n; i++)
  103.         res = pf2(res, pf(i));
  104.     return res;
  105. }
  106.  
  107. function<int(int)> partapply(function<int(int, int, int, function<int(int)>, function<int(int, int)>)> af, int m, int start, function<int(int)> pf, function<int(int, int)> pf2)
  108. {
  109.     return [af, m, start, pf, pf2](int n)->int {return af(m, n, start, pf, pf2); };
  110. }
  111.  
  112. int memoise(function<int(int)> pf, int n, int* a)
  113. {
  114.     if (a[n] == 0)
  115.         a[n] = pf(n);
  116.     return a[n];
  117. }
  118.  
  119. function<int(int)> partapply(function<int(function<int(int)>, int, int*)> mf, function<int(int)> pf, int* a)
  120. {
  121.     return [mf, pf, a](int n)->int {return mf(pf, n, a); };
  122. }
  123.  
  124. void main()
  125. {
  126.     int n;
  127.     cin >> n;
  128.     int a[100];
  129.     for (int i = 0; i < 100; i++)
  130.         a[i] = 0;
  131. //  function<int(int)> fact = partapply(alpha, 1, 1, [](int x)->int {return x; }, [](int x, int y)->int {return x * y; });
  132. //  function<int(int)> memfact = partapply(memoise, fact, a);
  133. //  cout << alpha(1, n, 0, memfact, [](int x, int y)->int {return x + y; }) << endl;
  134.     cout << alpha(1, n, 0, partapply(memoise, partapply(alpha, 1, 1, [](int x)->int {return x; }, [](int x, int y)->int {return x * y; }), a), [](int x, int y)->int {return x + y; }) << endl;
  135.     system("pause");
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top