Created
March 17, 2019 19:04
-
-
Save dlbas/bd2c5d34f3260c00f1162ade041eb887 to your computer and use it in GitHub Desktop.
lamda functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
#include <string> | |
#include <vector> | |
#include <map> | |
// 4 24 +2 + остаток от деления x на 5 +1 9 или 18 | |
static int count, branch_count; | |
std::function<void (int, int, std::string)> calculate = [](int x, int y, std::string path) { | |
branch_count++; | |
if (x < y) { | |
calculate(x + 2, y, path + "+2 "); | |
if (x % 5 != 0) { | |
calculate(x + (x % 5) + 1, y, path + "+(x%5)+1 "); | |
} | |
} else if (x == y) { | |
count++; | |
std::cout << path << "\n"; | |
} | |
}; | |
std::function<void (int, int, std::string)> calculate_reverse = [](int x, int y, std::string path) { | |
branch_count++; | |
if (x < y) { | |
calculate_reverse(x, y - 2, path + "-2 "); | |
if (y % 5 != 1) { | |
std::string t = ""; | |
for (auto i = (y - 1) % 5; i < y; i += 5) { | |
calculate_reverse(x, y - i, path + "-(y%5)" + t + " "); | |
t = t + "-5"; | |
} | |
} | |
} else if (x == y) { | |
count++; | |
std::cout << path << "\n"; | |
} | |
}; | |
static const int goal1 = 9, goal2 = 18; | |
std::function<void (int, int, std::string, int)> calculate_with_goals = [](int x, int y, std::string path, bool pass) { | |
branch_count++; | |
if (x <= goal2 || pass == true) { | |
if (x == goal1 || x == goal2) { | |
pass = true; | |
} | |
if (x < y) { | |
calculate_with_goals(x + 2, y, path + "+2 ", pass); | |
if (x % 5 != 0) { | |
calculate_with_goals(x + (x % 5) + 1, y, path + "+(x%5)+1 ", pass); | |
} | |
} else if (x == y) { | |
count++; | |
std::cout << path << "\n"; | |
} | |
} | |
}; | |
static std::map<int, int> map; | |
std::function<int (int, int, std::string)> calculate_with_research = [](int x, int y, std::string path) { | |
branch_count++; | |
int this_count = 0; | |
auto it = map.find(x); | |
if (it != map.end()) { | |
count += it->second; | |
this_count = it->second; | |
} else { | |
if (x < y) { | |
this_count += calculate_with_research(x + 2, y, path + "+2 "); | |
if (x % 5 != 0) { | |
this_count += calculate_with_research(x + (x % 5) + 1, y, path + "+(x%5)+1 "); | |
} | |
} else if (x == y) { | |
count++; | |
this_count++; | |
} | |
} | |
map.insert(std::pair<int, int>(x, this_count)); | |
return this_count; | |
}; | |
int main() { | |
int x, y; | |
std::cin >> x >> y; | |
count = 0; branch_count = 0; | |
calculate(x, y, ""); | |
// 41; 267 | |
std::cout << "Count: " << count << "\n"; | |
std::cout << "Branch Count: " << branch_count << "\n"; | |
std::cout << "\n"; | |
// ------------------------------------------------------------ | |
count = 0; branch_count = 0; | |
calculate_reverse(x, y, ""); | |
// 33; 429 | |
std::cout << "Count: " << count << "\n"; | |
std::cout << "Branch Count: " << branch_count << "\n"; | |
std::cout << "\n"; | |
// ------------------------------------------------------------ | |
count = 0; branch_count = 0; | |
calculate_with_goals(x, y, "", false); | |
// 29; 191 | |
std::cout << "Count: " << count << "\n"; | |
std::cout << "Branch Count: " << branch_count << "\n"; | |
std::cout << "\n"; | |
// ------------------------------------------------------------ | |
count = 0; branch_count = 0; | |
calculate_with_research(x, y, ""); | |
// 41; 34 | |
std::cout << "Count: " << count << "\n"; | |
std::cout << "Branch Count: " << branch_count << "\n"; | |
std::cout << "\n"; | |
for (auto it = map.begin(); it != map.end(); it++) { | |
std::cout << it->first << ": " << it->second << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment