EWTD

template for solving 6th problem in russian state exam

Sep 19th, 2019
183
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <type_traits>
  5. #include <sstream>
  6. #include <functional>
  7.  
  8. template <typename T>
  9. struct Request{
  10.     static_assert(std::is_integral<T>::value,"must be integral type");
  11.     Request(T l_b = 0, T r_b = 0, T v = 0):
  12.             left_border(l_b),
  13.             right_border(r_b),
  14.             value_to_find(v)
  15.     {}
  16.     T distanse() const {
  17.         return abs(left_border-right_border);
  18.     }
  19.     const T left_border;
  20.     const T right_border;
  21.     const T value_to_find;
  22. };
  23. template <typename T>
  24. struct Strategy{
  25.     static_assert(std::is_integral<T>::value,"must be integral type");
  26.     Strategy(const std::string& name = "Strategy"): _name(name){}
  27.     virtual void next(T& value) const = 0;
  28.     virtual T next(const T& value) const = 0;
  29.     virtual void prev(T& value) const  = 0;
  30.     virtual T prev(const T& value) const = 0;
  31.     const std::string& name(){
  32.         return _name;
  33.     }
  34. private:
  35.     const std::string _name;
  36. };
  37. template <typename T>
  38. struct Forward_Enum_Strategy: public Strategy<T>{
  39.     static_assert(std::is_integral<T>::value,"must be integral type");
  40.     Forward_Enum_Strategy(): Strategy<T>("Enum_strategy"){}
  41.     void next(T& value)const  override {
  42.         value++;
  43.     }
  44.     T next(const T& value) const override {
  45.         return value+1;
  46.     }
  47.     void prev(T& value) const override {
  48.         value--;
  49.     }
  50.     T prev(const T& value) const override{
  51.         return value-1;
  52.     }
  53. };
  54. template <typename T>
  55. struct Backward_Enum_Strategy: public Strategy<T>{
  56.  
  57. };
  58. template<typename T, typename F, typename Action>
  59. void for_each_in_range(const Request<T>& request, const Strategy<T>& strategy, F change, Action action){
  60.     assert(!(request.left_border > request.right_border && dynamic_cast<const Forward_Enum_Strategy<T>*>(&strategy)));
  61.     assert(!(request.left_border < request.right_border && dynamic_cast<const Backward_Enum_Strategy<T>*>(&strategy)));
  62.     for(T iterator = request.left_border; iterator <= request.right_border; strategy.next(iterator)){
  63.         if(request.value_to_find == change(iterator)){
  64.             action(iterator);
  65.         }
  66.     }
  67. }
  68. template <typename T, typename F>
  69. std::pair<bool, unsigned long long> find_in_range(const Request<T>& request, const Strategy<T>& strategy, F change){
  70.     unsigned long long applied_values = 0;
  71.     bool success = false;
  72.     auto action = [&applied_values, &success](T value){
  73.         applied_values++;
  74.         success = true;
  75.     };
  76.     for_each_in_range(request,strategy, change, action);
  77.     return std::make_pair(success, applied_values);
  78. }
  79. template <typename T, typename F>
  80. bool find_in_range(const Request<T>& request, const Strategy<T>& strategy, F change){
  81.     bool success = false;
  82.     auto action = [&success](T value){
  83.         success = true;
  84.     };
  85.     for_each_in_range(request,strategy,change,action);
  86.     return success;
  87. }
  88. template <typename T, typename F>
  89. unsigned long long find_in_range(const Request<T>& request, const Strategy<T>& strategy, F change){
  90.     unsigned long long applied_values = 0;
  91.     auto action = [&applied_values](T value){
  92.         applied_values++;
  93.     };
  94.     for_each_in_range(request, strategy, change, action);
  95.     return applied_values;
  96. }
  97. template <typename T,typename F>
  98. T find_first_of(const Request<T>& request, const Strategy<T>& strategy, F change){
  99.     for(T iterator = request.left_border; iterator <= request.right_border; strategy.next(ref(iterator))){
  100.         if(request.value_to_find == change(iterator)){
  101.             return iterator;
  102.         }
  103.     }
  104.     return T();
  105. }
  106. template <typename T,typename F>
  107. T find_last_of(const Request<T>& request, Strategy<T>& strategy, F change){
  108.     for(T iterator = request.right_border; iterator >= request.left_border; strategy.prev(iterator)){
  109.         if(request.value_to_find == change(iterator)){
  110.             return iterator;
  111.         }
  112.     }
  113.     return T();
  114. }
  115. template <typename T,typename F>
  116. std::vector<T> find_all(const Request<T>& request, const Strategy<T>& strategy, F change){
  117.     std::vector<T> result;
  118.     result.reserve(request.distanse());
  119.     auto action = [&result](T value){
  120.         result.push_back(value);
  121.     };
  122.     for_each_in_range(request, strategy, change, action);
  123.     return result;
  124. }
  125. template<typename T>
  126. std::vector<int> value_to_digits(T value){
  127.     static_assert(std::is_integral<T>::value,"must be integral type");
  128.     std::vector<int> digits;
  129.     digits.reserve(20);
  130.     while(value){
  131.         digits.push_back(value%10);
  132.         value /= 10;
  133.     }
  134.     reverse(digits.begin(),digits.end());
  135.     return digits;
  136. }
  137. int main(){
  138.     using namespace std;
  139.     Request request(1000,9999,1515);
  140.     Forward_Enum_Strategy<int> strategy;
  141.     auto change = [](long long value){
  142.         auto digits = value_to_digits(value);
  143.         int a = digits[0] + digits[1];
  144.         int b = digits[1] + digits[2];
  145.         int c = digits[2] + digits[3];
  146.         int prod_a = max(a,max(b,c));
  147.         int prod_b = a+b+c - prod_a - min(a,min(b,c));
  148.         if(prod_a > prod_b){
  149.             return stoi(to_string(prod_b) + to_string(prod_a));
  150.         }else{
  151.             return stoi(to_string(prod_a) + to_string(prod_b));
  152.         }
  153.     };
  154.     cout << "{ ";
  155.     for(auto x: find_all(request,strategy,change)){
  156.         cout << x << ", ";
  157.     }
  158.     cout << "}\n";
  159.     return 0;
  160. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×