Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cassert>
- #include <type_traits>
- #include <sstream>
- #include <functional>
- template <typename T>
- struct Request{
- static_assert(std::is_integral<T>::value,"must be integral type");
- Request(T l_b = 0, T r_b = 0, T v = 0):
- left_border(l_b),
- right_border(r_b),
- value_to_find(v)
- {}
- T distanse() const {
- return abs(left_border-right_border);
- }
- const T left_border;
- const T right_border;
- const T value_to_find;
- };
- template <typename T>
- struct Strategy{
- static_assert(std::is_integral<T>::value,"must be integral type");
- Strategy(const std::string& name = "Strategy"): _name(name){}
- virtual void next(T& value) const = 0;
- virtual T next(const T& value) const = 0;
- virtual void prev(T& value) const = 0;
- virtual T prev(const T& value) const = 0;
- const std::string& name(){
- return _name;
- }
- private:
- const std::string _name;
- };
- template <typename T>
- struct Forward_Enum_Strategy: public Strategy<T>{
- static_assert(std::is_integral<T>::value,"must be integral type");
- Forward_Enum_Strategy(): Strategy<T>("Enum_strategy"){}
- void next(T& value)const override {
- value++;
- }
- T next(const T& value) const override {
- return value+1;
- }
- void prev(T& value) const override {
- value--;
- }
- T prev(const T& value) const override{
- return value-1;
- }
- };
- template <typename T>
- struct Backward_Enum_Strategy: public Strategy<T>{
- };
- template<typename T, typename F, typename Action>
- void for_each_in_range(const Request<T>& request, const Strategy<T>& strategy, F change, Action action){
- assert(!(request.left_border > request.right_border && dynamic_cast<const Forward_Enum_Strategy<T>*>(&strategy)));
- assert(!(request.left_border < request.right_border && dynamic_cast<const Backward_Enum_Strategy<T>*>(&strategy)));
- for(T iterator = request.left_border; iterator <= request.right_border; strategy.next(iterator)){
- if(request.value_to_find == change(iterator)){
- action(iterator);
- }
- }
- }
- template <typename T, typename F>
- std::pair<bool, unsigned long long> find_in_range(const Request<T>& request, const Strategy<T>& strategy, F change){
- unsigned long long applied_values = 0;
- bool success = false;
- auto action = [&applied_values, &success](T value){
- applied_values++;
- success = true;
- };
- for_each_in_range(request,strategy, change, action);
- return std::make_pair(success, applied_values);
- }
- template <typename T, typename F>
- bool find_in_range(const Request<T>& request, const Strategy<T>& strategy, F change){
- bool success = false;
- auto action = [&success](T value){
- success = true;
- };
- for_each_in_range(request,strategy,change,action);
- return success;
- }
- template <typename T, typename F>
- unsigned long long find_in_range(const Request<T>& request, const Strategy<T>& strategy, F change){
- unsigned long long applied_values = 0;
- auto action = [&applied_values](T value){
- applied_values++;
- };
- for_each_in_range(request, strategy, change, action);
- return applied_values;
- }
- template <typename T,typename F>
- T find_first_of(const Request<T>& request, const Strategy<T>& strategy, F change){
- for(T iterator = request.left_border; iterator <= request.right_border; strategy.next(ref(iterator))){
- if(request.value_to_find == change(iterator)){
- return iterator;
- }
- }
- return T();
- }
- template <typename T,typename F>
- T find_last_of(const Request<T>& request, Strategy<T>& strategy, F change){
- for(T iterator = request.right_border; iterator >= request.left_border; strategy.prev(iterator)){
- if(request.value_to_find == change(iterator)){
- return iterator;
- }
- }
- return T();
- }
- template <typename T,typename F>
- std::vector<T> find_all(const Request<T>& request, const Strategy<T>& strategy, F change){
- std::vector<T> result;
- result.reserve(request.distanse());
- auto action = [&result](T value){
- result.push_back(value);
- };
- for_each_in_range(request, strategy, change, action);
- return result;
- }
- template<typename T>
- std::vector<int> value_to_digits(T value){
- static_assert(std::is_integral<T>::value,"must be integral type");
- std::vector<int> digits;
- digits.reserve(20);
- while(value){
- digits.push_back(value%10);
- value /= 10;
- }
- reverse(digits.begin(),digits.end());
- return digits;
- }
- int main(){
- using namespace std;
- Request request(1000,9999,1515);
- Forward_Enum_Strategy<int> strategy;
- auto change = [](long long value){
- auto digits = value_to_digits(value);
- int a = digits[0] + digits[1];
- int b = digits[1] + digits[2];
- int c = digits[2] + digits[3];
- int prod_a = max(a,max(b,c));
- int prod_b = a+b+c - prod_a - min(a,min(b,c));
- if(prod_a > prod_b){
- return stoi(to_string(prod_b) + to_string(prod_a));
- }else{
- return stoi(to_string(prod_a) + to_string(prod_b));
- }
- };
- cout << "{ ";
- for(auto x: find_all(request,strategy,change)){
- cout << x << ", ";
- }
- cout << "}\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement