Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. using namespace std;
  7. const int start = 5, fin = 38; // Начальное и конечное числа
  8. int main()
  9. {
  10.     // Прямой ход расчета
  11.     int branchCount = 0; // Счётчик просмотренных веток дерева
  12.     // function<int(int, int, string)> calc = [&calc, &branchCount](int x, int fin, string way) {
  13.     //     branchCount++;
  14.     //     if(x<fin)
  15.     //         return calc(x + 4, fin, way + " +4") + calc(x * 2, fin, way + " *2");
  16.     //     else
  17.     //     if(x==fin)
  18.     //     {
  19.     //         cout<<way<<endl;
  20.     //         return 1;
  21.     //     }
  22.     //     return 0;
  23.     // };
  24.     // cout << "Function#1" << endl;
  25.     // cout << "From " << start << " to " << fin << endl;
  26.     // cout << "Allowed operations: +4, *2" << endl;
  27.     // cout << "Variants:" << endl
  28.     //      << calc(start, fin, "") << endl;
  29.     // cout << "branchCount:" << branchCount << endl;
  30.     // function<int(int, int, string)> reverseCalc = [&reverseCalc, &branchCount](int x, int fin, string way) {
  31.     //     branchCount++;
  32.     //     if (x > fin)
  33.     //         if ( x % 2 == 0)
  34.     //             return reverseCalc(x - 4, fin, way + " -4") + reverseCalc(x / 2, fin, way + " /2");
  35.     //         else
  36.     //             return reverseCalc(x - 4, fin, way + " -4");        
  37.     //     else if (x == fin)
  38.     //     {
  39.     //         cout << way << endl;
  40.     //         return 1;
  41.     //     }
  42.     //     return 0;
  43.     // };
  44.     // branchCount = 0;
  45.     // cout << endl << "Function#2" << endl;
  46.     // cout << "From " << fin << " to " << start << endl;
  47.     // cout << "Allowed operations: -4, /2" << endl;
  48.     // cout << "Variants:" << endl
  49.     //      << reverseCalc(fin, start, "") << endl;
  50.     // cout << "branchCount:" << branchCount << endl;
  51.  
  52.     // function<int(int, int, string, bool)> Restrcalc = [&Restrcalc, &branchCount](int x, int fin, string way, bool restrictionCondition) {
  53.     //     branchCount++;
  54.     //     if (x == 34)
  55.     //         restrictionCondition = true;
  56.     //     if (x > 34 && restrictionCondition == false)
  57.     //         return 0;
  58.     //     if (x < fin)
  59.     //         return Restrcalc(x + 4, fin, way + " +4", restrictionCondition) + Restrcalc(x * 2, fin, way + " *2", restrictionCondition);
  60.     //     else if (x == fin && restrictionCondition)
  61.     //     {
  62.     //         cout << way << endl;
  63.     //         return 1;
  64.     //     }
  65.     //     return 0;
  66.     // };
  67.  
  68.     // branchCount = 0;
  69.     // cout <<  endl << "Function#3" << endl;
  70.     // cout << "From " << fin << " to " << start << " passing throught 34" << endl;
  71.     // cout << "Allowed operations: -4, /2" << endl;
  72.     // cout << "Variants:" << endl
  73.     //      << Restrcalc(start, fin, "", false) << endl;
  74.     // cout << "branchCount:" << branchCount << endl;
  75.  
  76.     // function<int(int, int, string, bool)> revRestrcalc = [&revRestrcalc, &branchCount](int x, int fin, string way, bool restrictionCondition) {
  77.     //     branchCount++;
  78.     //     if (x == 34)
  79.     //         restrictionCondition = true;
  80.     //     if (x < 34 && restrictionCondition == false)
  81.     //         return 0;
  82.     //     if (x > fin)
  83.     //         if (x % 2 == 0)
  84.     //             return revRestrcalc(x - 4, fin, way + " -4", restrictionCondition) + revRestrcalc(x / 2, fin, way + " /2", restrictionCondition);
  85.     //         else
  86.     //             return revRestrcalc(x - 4, fin, way + " -4", restrictionCondition);
  87.     //     else if (x == fin && restrictionCondition)
  88.     //     {
  89.     //         cout << way << endl;
  90.     //         return 1;
  91.     //     }
  92.     //     return 0;
  93.     // };
  94.  
  95.     // branchCount = 0;
  96.     // cout << endl << "Function#4" << endl;
  97.     // cout << "From " << fin << " to " << start << " passing throught 34" << endl;
  98.     // cout << "Allowed operations: -4, /2" << endl;
  99.     // cout << "Variants:" << endl
  100.     //      << revRestrcalc(fin, start, "", false) << endl;
  101.     // cout << "branchCount:" << branchCount << endl;
  102.     branchCount = 0;
  103.     map<int, vector<string>> phase;
  104.     function<int(int, int, string)> calc = [&calc, &branchCount, &phase](int x, int fin, string way) {
  105.         branchCount++;
  106.         if(x<fin)
  107.             return calc(x + 4, fin, way + " +4") + calc(x * 2, fin, way + " *2");
  108.         else
  109.         if(x==fin)
  110.         {
  111.             cout<<way<<endl;
  112.             return 1;
  113.         }
  114.         return 0;
  115.     };
  116.     auto result = phaseCalc(start, fin, "", {});
  117.     cout << "Branch count: " << branchCount << endl;
  118.     cout << "Result len: " << result.size() << endl;
  119.     for (auto s : result)
  120.         cout << s << endl;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement